I am trying to use dplyr's group_by in a local function, example:
testFunction <- function(df, x) {
df %>%
group_by(x) %>%
summarize(mean.Petal.Width = mean(Petal.Width))
}
testFunction(iris, Species)
and I get an error "... unknown variable to group by: x" I've tried group_by_ and it gives me a summary of the entire dataset. Anybody have a clue how I can fix this?
Thanks in advance!
I got it to work like this:
I changed
x
toget(x)
, andSpecies
to"Species"
intestFunction(iris,...)
.Here is one way to work with the new
enquo
fromdplyr
, whereenquo
takes the string and converts toquosure
which gets evaluated by unquoting (UQ
or!!
) ingroup_by
,mutate
,summarise
etc.