I have read several guides on programming with dplyr now and I am still confused about how to solve the problem of evaluating constructed/concatenated strin
Use sym
and :=
like this:
library(dplyr)
library(rlang)
t <- tibble( x_01 = c(1, 2, 3), x_02 = c(4, 5, 6))
i <- 1
new <- sym(sprintf("d_%02d", i))
var <- sym(sprintf("x_%02d", i))
t %>% mutate(!!new := (!!var) * 2)
giving:
# A tibble: 3 x 3
x_01 x_02 d_01
1 1 4 2
2 2 5 4
3 3 6 6
Also note that this is trivial in base R:
tdf <- data.frame( x_01 = c(1, 2, 3), x_02 = c(4, 5, 6))
i <- 1
new <- sprintf("d_%02d", i)
var <- sprintf("x_%02d", i)
tdf[[new]] <- 2 * tdf[[var]]