Using the package dplyr and the function sample_frac it is possible to sample a percentage from every group. What I need is to first sort the elements in every
I know this is coming late, but might help someone now. dplyr has a new function top_frac
library(dplyr)
mtcars %>%
select(gear, wt) %>%
group_by(gear) %>%
arrange(gear, wt) %>%
top_frac(n = 0.2,wt = wt)
Here n is the fraction of rows to return and wt is the variable to be used for ordering.
The output is as below.
gear wt
3 5.250
3 5.345
3 5.424
4 3.440
4 3.440
5 3.570