dplyr - filter by group size

后端 未结 6 1909
渐次进展
渐次进展 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 15:53

    I know you asked for a dplyr solution but if you combine it with some purrr you can get it in one line without specifying any new functions. (A little slower though.)

    library(dplyr)
    library(purrr)
    library(tidyr)
    
    dat %>% 
      group_by(cat) %>% 
      nest() %>% 
      mutate(n = map(data, n_distinct)) %>%
      unnest(n = n) %>% 
      filter(n == 5) %>% 
      select(cat, n)
    

提交回复
热议问题