| library(tidyverse) | |
| keep_top_n <- function(df, col, n = 10) { | |
| semi_join(df, head(count_(df, col, sort = TRUE), n)) | |
| } | |
| data(mpg) | |
| # All car models | |
| mpg %>% nrow() | |
| # Just car models of top three manufacturers | |
| mpg %>% keep_top_n(~manufacturer, 3) %>% nrow() |