I see the answers on this website, but it can not solve my problem.
What I want is that use dynamic variable names both on LHS and RHS within summarize
.
This is a simple example to show what I have tried:
why I use paste0('carb')
not use carb
directly is that on the position(paste0('carb')
) is a dynamic variable like this paste0('temp', n)
and n
is a series of numbers in my real situation.
library(dplyr)
sumay1 <- mtcars %>% group_by(cyl) %>%
summarise(!!paste0('carb', 100) := mean(paste0('carb'), na.rm = T))
sumay2 <- mtcars %>% group_by(cyl) %>%
summarise(!!paste0('carb', 100) := mean(sym('carb'), na.rm = T))
sumay3 <- mtcars %>% group_by(cyl) %>%
summarise(!!paste0('carb', 100) := mean({{paste0('carb')}}, na.rm = T))