Reputation: 65
I've read most of the documentation about tidy evaluation and programming with dplyr but cannot get my head around this (simple) problem.
I want to programm with dplyr and give column names as strings as input to the function.
df <- tibble(
g1 = c(1, 1, 2, 2, 2),
g2 = c(1, 2, 1, 2, 1),
a = sample(5),
b = sample(5)
)
my_summarise <- function(df, group_var) {
df %>%
group_by(group_var) %>%
summarise(a = mean(a))
}
my_summarise(df, 'g1')
This gives me Error : Column 'group_var' is unknown
.
What must I change inside the my_summarise function in order to make this work?
Upvotes: 3
Views: 1192
Reputation: 2332
You can also use sym
and !!
my_summarise <- function(df, group_var) {
df %>%
group_by(!!sym(group_var)) %>%
summarise(a = mean(a))
}
my_summarise(df, 'g1')
# A tibble: 2 x 2
g1 a
<dbl> <dbl>
1 1 3.5
2 2 2.67
Upvotes: 1
Reputation: 887941
We can use also ensym
with !!
my_summarise <- function(df, group_var) {
df %>%
group_by(!!rlang::ensym(group_var)) %>%
summarise(a = mean(a))
}
my_summarise(df, 'g1')
Or another option is group_by_at
my_summarise <- function(df, group_var) {
df %>%
group_by_at(vars(group_var)) %>%
summarise(a = mean(a))
}
my_summarise(df, 'g1')
Upvotes: 3
Reputation: 652
Convert the string column name to a bare column name using as.name()
and then use the new {{}} (read Curly-Curly) operator as below:
library(dplyr)
df <- tibble(
g1 = c(1, 1, 2, 2, 2),
g2 = c(1, 2, 1, 2, 1),
a = sample(5),
b = sample(5)
)
my_summarise <- function(df, group_var) {
grp_var <- as.name(group_var)
df %>%
group_by({{grp_var}}) %>%
summarise(a = mean(a))
}
my_summarise(df, 'g1')
Upvotes: 4