I like to select the first (2,3,0,4) rows of each group in a data frame.
> f<-data.frame(group=c(1,1,1,2,2,3,4),y=c(1:7))
>
> group y
> 1 1
> 1 2
> 1 3
> 2 4
> 2 5
> 3 6
> 4 7
and obtain a data frame as follows
group y
1 1
1 2
2 4
2 5
4 7
I tried to use by and head but head does not take a vector.
Thank you for your help.
Version of function with indexes.
Test data (size suggested by author of question)
system.time(fun1())
system.time(fun2())
On my system ~60 times faster is fun1.