Last active
February 24, 2017 21:24
-
-
Save sharan-naribole/d1c1e647451a38f355c99875eaaa1b9d to your computer and use it in GitHub Desktop.
dplyr lazyeval sample
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
example <- function(df, column_name, input_vec) { | |
# INPUTS: | |
# df : data frame | |
# column_name : string value of column name in df | |
# input_vec : character vector of values in column_name of df | |
# OUTPUTS: | |
# df : filtered dataframe | |
# Main reason for using this is because df$column_name throws | |
# error as column_name is not a column in df | |
# We need the column_name input to be used | |
# Expressing the dplyr statement | |
# column_name %in% input_vec using interp | |
filter_criteria <- interp(~x %in% y, .values = list(x = as.name(column_name), y = input_vec)) | |
# filter_ instead of filter | |
df %>% | |
filter_(filter_criteria) -> df | |
return(df) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment