Skip to content

Instantly share code, notes, and snippets.

@oganm
Last active December 6, 2019 00:32
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 oganm/276c64eba596238fc1531ce60af84c0b to your computer and use it in GitHub Desktop.
Save oganm/276c64eba596238fc1531ce60af84c0b to your computer and use it in GitHub Desktop.
dplyr standard evaluation recipes
library(dplyr)
diamonds = ggplot2::diamonds
filterBy = 'color'
diamonds %>% filter(!!rlang::sym(filterBy) == 'E')
diamonds %>% group_by(!!rlang::sym(filterBy))
diamonds %>% mutate(!!rlang::sym('new_col'):='yes')
# take in name
# quote is the same thing as substitute
group_by2 = function(df,group_var){
group_var = enquo(group_var)
df %>% group_by(!! group_var)
}
diamonds %>% group_by2(color)
# new syntax
group_by3 = function(df,group_var){
df %>% group_by({{group_var}})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment