Skip to content

Instantly share code, notes, and snippets.

@bearloga
Created March 16, 2016 23:24
Show Gist options
  • Select an option

  • Save bearloga/08adfd1e82ed456cdfdc to your computer and use it in GitHub Desktop.

Select an option

Save bearloga/08adfd1e82ed456cdfdc to your computer and use it in GitHub Desktop.
Augments dplyr::top_n() to also return a summary of the remainder of the dataset that didn't make the cut. Useful when working with proportions.
library(magrittr)
library(dplyr)
library(stringr)

df <- mtcars; df$car <- rownames(df)

df %>%
  mutate(make = str_extract(car, "^[A-Za-z]+\\b")) %>%
  group_by(make) %>%
  summarize(n = n()) %>%
  mutate(proportion = n/sum(n)) %>%
  arrange(desc(proportion)) %>%
  top_n2(5, proportion, make) %>%
  select(-n) %>%
  mutate(proportion = sprintf("%.2f%%", 100*proportion)) %>%
  knitr::kable()
make proportion
Merc 21.88%
Fiat 6.25%
Hornet 6.25%
Mazda 6.25%
Toyota 6.25%
...17 others... 53.12%
library(magrittr)
library(dplyr)
top_n2 <- function(x, n, wt, var, f = sum) {
expr <- paste0("dplyr::top_n(x, ", n, ", ", deparse(substitute(wt)), ")")
y <- eval(parse(text = expr))
`%notin%` <- function(x, y) {
return(!(x %in% y))
}
filter_dots <- eval(parse(text = paste0("list(~", deparse(substitute(var)), "%notin% y$", deparse(substitute(var)), ")")))
summary_dots <- eval(parse(text = paste0("list(~paste0('...', length(", deparse(substitute(var)),
"), ' others...'), ~f(", deparse(substitute(wt)), "))")))
z <- x %>%
filter_(.dots = filter_dots) %>%
summarize_(.dots = summary_dots)
names(z) <- c(deparse(substitute(var)), deparse(substitute(wt)))
return(bind_rows(y, z))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment