I have dataframe like below :-
x<-c(3,2,1,8,7,11,10,9,7,5,4)
y<-c(\"a\",\"a\",\"a\", \"b\",\"b\",\"c\",\"c\",\"c\",\"c\",\"c\",\"c\")
z<-c(2,2,2,1,1
I'm posting the solution I was looking for using dplyr. It is based on @HNSKD:
library(dplyr)
x<-c(3,2,1,8,7,11,10,9,7,5,4)
y<-c("a","a","a", "b","b","c","c","c","c","c","c")
z<-c(2,2,2,1,1,3,3,3,3,3,3)
df<-data.frame(x,y,z)
df %>% group_by(y) %>% slice(1:2)
Which returns the first two elements for each y
:
# A tibble: 6 x 3
# Groups: y [3]
x y z
1 3 a 2
2 2 a 2
3 8 b 1
4 7 b 1
5 11 c 3
6 10 c 3