Example data:
(tmp_df <-
expand.grid(id = letters[1:3], y = 1:3))
# id y
# 1 a 1
# 2 b 1
# 3 c 1
# 4 a 2
# 5 b 2
# 6 c 2
# 7 a 3
# 8 b 3
# 9 c 3
The following works:
tmp_df %>%
group_by(id) %>%
mutate_at(which(colnames(.) %in% c("y")),
sum)
# id y
# <fct> <int>
# 1 a 6
# 2 b 6
# 3 c 6
# 4 a 6
# 5 b 6
# 6 c 6
# 7 a 6
# 8 b 6
# 9 c 6
but the following throws the error Error: Only strings can be converted to symbols
:
tmp_df %>%
group_by(id) %>%
summarise_at(which(colnames(.) %in% c("y")),
sum)
Note that the following code snippets are alternative ways that successfully generate the expected result:
tmp_df %>%
group_by(id) %>%
summarise_at(vars(y),
sum)
tmp_df %>%
group_by(id) %>%
summarise_at("y",
sum)
EDIT: following akrun's answer I should note that the dplyr version I am using is dplyr_0.8.4