dplyr - filter by group size

后端 未结 6 1910
渐次进展
渐次进展 2020-11-28 15:12

What is the best way to filter a data.frame to only get groups of say size 5?

So my data looks as follows:

require(dplyr)
n <- 1e5
x <- rnorm(n         


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 16:04

    You can do it more concisely with n():

    library(dplyr)
    dat %>% group_by(cat) %>% filter(n() == 5)
    

提交回复
热议问题