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