dplyr - Group by and select TOP x %

后端 未结 5 1867
天命终不由人
天命终不由人 2020-12-01 11:31

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

5条回答
  •  时光说笑
    2020-12-01 11:50

    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().

提交回复
热议问题