-
-
Save oganm/276c64eba596238fc1531ce60af84c0b to your computer and use it in GitHub Desktop.
dplyr standard evaluation recipes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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