I'm trying to get a custom group_by function working using quosure. It works fine when the input into the function is the name of a field. However, if I want to create a variable that contains the name of the field to insert into the function, I get stuck because the field is quoted. Anyone know how to get around this. Please see below
groupedMean<-function(field){
expr<-enquo(field)
mtcars%>%
group_by(!!expr)%>%
summarise(mean(mpg))
}
#Works
groupedMean(cyl)
#Doesn't work
groupFields<-c("cyl","gear")
for(var in groupFields){
print(groupedMean(eval(var)))
}