Skip to content

Instantly share code, notes, and snippets.

@Keiku
Created March 10, 2017 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Keiku/b9868b25ce20655c75f58b10e5510758 to your computer and use it in GitHub Desktop.
Save Keiku/b9868b25ce20655c75f58b10e5510758 to your computer and use it in GitHub Desktop.
Summarising by standard evaluation with dplyr.
library(dplyr)
library(lazyeval)
df <- data_frame(group = c(1, 2, 2, 3, 3, 3))
g <- "group"
df %>%
group_by_(g) %>%
summarise_(
n = "n()",
sum = interp(~sum(col, na.rm = TRUE), col = as.name(g))
)
# A tibble: 3 × 3
# group n sum
# <dbl> <int> <dbl>
# 1 1 1 1
# 2 2 2 4
# 3 3 3 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment