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
sample_frac
I believe this gets to the answer you're looking for.
library(dplyr) mtcars %>% select(gear, wt) %>% group_by(gear) %>% arrange(gear, wt) %>% filter(row_number() / n() <= .2)