Skip to content

Instantly share code, notes, and snippets.

@sdtaylor
Created February 8, 2017 22:31
Show Gist options
  • Save sdtaylor/40e95fa8fa32e5dd7c8989d087e450d4 to your computer and use it in GitHub Desktop.
Save sdtaylor/40e95fa8fa32e5dd7c8989d087e450d4 to your computer and use it in GitHub Desktop.
Use dplyr inside a function with arbitray column names
#Use dplyr inside a function with arbitray column names
#The dots stuff is from the internal workings of dplyr
#Don't forget the _ at the end of the group by.
#Other function also have the _ variation with the .dots argument available,
#but when I first made this in early 2016 not all of them did.
my_function=function(original_df, variables){
dots=lapply(variables, as.symbol)
summarized_data = original_data %>%
group_by_(.dots = dots ) %>%
summarize(freq=sum(freq)) %>%
ungroup()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment