Reputation: 121
So I am taking a column name which is guaranteed to exist in the data frame as the input, and want to calculate the mean of the given column (Let's suppose c is the parameter in the function).
EDIT: Tried recreating a scenario here.
states <- c("Washington", "Washington", "California", "California")
random.data <- c(10, 20, 30, 40)
data <- data.frame(states, random.data)
data.frame <- data %>%
group_by(states) %>%
summarise_(average = mean(paste0("random", ".data")))
When I tried doing this I got the following error:
Warning message:
In mean.default(paste0("random", ".data")) :
argument is not numeric or logical: returning NA
I have an idea about standard evaluation, but for some reason it is not working for this mean function.
Upvotes: 1
Views: 39
Reputation: 887891
We can use sym
with !!
col <- paste0("random", ".data")
data %>%
group_by(states) %>%
summarise(average = mean(!! rlang::sym(col)))
##or use directly
#summarise(average = mean(!! rlang::sym(paste0("random", ".data"))))
# A tibble: 2 x 2
# states average
# <fctr> <dbl>
#1 California 35
#2 Washington 15
Or another option is get
data %>%
group_by(states) %>%
summarise(average = mean(get(paste0("random", ".data"))))
# A tibble: 2 x 2
# states average
# <fctr> <dbl>
#1 California 35
#2 Washington 15
Upvotes: 1