We can use group_by_at
which can accept string argument.
library(tidyverse)
c("hp", "cyl") %>%
tibble() %>%
magrittr::set_colnames("vars1") %>%
mutate(data = map(vars1,~mtcars %>% as_tibble)) %>%
mutate(res = map2(data,vars1,function(x,y){
x %>%
group_by_at(y)
}))
# A tibble: 2 x 3
# vars1 data res
# <chr> <list> <list>
#1 hp <tibble [32 × 11]> <tibble [32 × 11]>
#2 cyl <tibble [32 × 11]> <tibble [32 × 11]>
The current solution works if we make it as a standalone function and apply it in map2
sym_fun <- function(x, y) {
x %>% group_by(!!sym(y))
}
c("hp", "cyl") %>%
tibble() %>%
magrittr::set_colnames("vars1") %>%
mutate(data = map(vars1,~mtcars %>% as_tibble)) %>%
mutate(res = map2(data,vars1,sym_fun))