Skip to content

Instantly share code, notes, and snippets.

Created April 12, 2017 10:12
What would you like to do?
R function to keep rows belonging to top n groups of a certain column
keep_top_n <- function(df, col, n = 10) {
semi_join(df, head(count_(df, col, sort = TRUE), n))
# All car models
mpg %>% nrow()
# Just car models of top three manufacturers
mpg %>% keep_top_n(~manufacturer, 3) %>% nrow()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment