Skip to content

Instantly share code, notes, and snippets.

@sharan-naribole
Last active February 24, 2017 21:24
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 sharan-naribole/d1c1e647451a38f355c99875eaaa1b9d to your computer and use it in GitHub Desktop.
Save sharan-naribole/d1c1e647451a38f355c99875eaaa1b9d to your computer and use it in GitHub Desktop.
dplyr lazyeval sample
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