What is the consensus on the best way to group_by when the group_by is being fed a variable? Consider the following simple function:
library(dplyr)
myFunction <- function(df,
col_name) {
out <-
group_by(col_name) %>%
summarize(mean = mean(mpg))
return(out)
}
myFunction(mtcars, col_name = c('cyl', 'am'))
The call to this function returns and error stating the column doesn't exist. I understand why but am not sure the best approach to get around this. I can make if work if only have one grouping variable by doing:
group_by(!!as.name(col_name))
This however doesn't work if col_name is a vector > 1
Any ideas?