In the current version of dplyr, select
arguments can be passed by value:
variable <- "Species"
iris %>%
select(variable)
# Species
#1 setosa
#2 setosa
#3 setosa
#4 setosa
#5 setosa
#6 setosa
#...
But group_by
arguments cannot be passed by value:
iris %>%
group_by(variable) %>%
summarise(Petal.Length = mean(Petal.Length))
# Error in grouped_df_impl(data, unname(vars), drop) :
# Column `variable` is unknown
The documented dplyr::select behaviour is
iris %>% select(Species)
And the documented documented dplyr::group_by behaviour is
iris %>%
group_by(Species) %>%
summarise(Petal.Length = mean(Petal.Length))
- Why are
select
andgroup_by
different with respect to passing arguments by value? - Why is the first
select
call working and will it continue to work in the future? - Why is the first
group_by
call not working? I'm trying to figure out what combination ofquo()
,enquo()
and!!
I should use to make it work.
I need this because I would like to create a function that takes a grouping variable as input parameter, if possible the grouping variable should be given as a character string, because two other function parameters are already given as character strings.
browseVignettes(package = "dplyr")
, you'll find one on programming, which covers what is/will be idiomatic, anyways. – Frankgroup_by(get(variable))
should get it to work but not sure whyselect
andgroup_by
are different in this respect. – pentandrous