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
Here's another way
mtcars %>% select(gear, wt) %>% arrange(gear, desc(wt)) %>% group_by(gear) %>% slice(seq(n()*.2)) gear wt (dbl) (dbl) 1 3 5.424 2 3 5.345 3 3 5.250 4 4 3.440 5 4 3.440 6 5 3.570
I take "top" to mean "having the highest value for wt" and so used desc().
wt
desc()