The best place to understand how to use strings in place of variable names is this Programming with dplyr .
You need to do something much like parse
and eval
that you quoted, but dplyr
, provides through the rlang
package, a more intuitive way. It is worth reading the rlang
site.
Suppose you have two strings representing variable names:
x <- "nova_coluna"
y <- "hp"
You can create a new column called nova_coluna
that equals the hp
variable as follows:
library(dplyr)
library(rlang)
y <- sym(y)
mtcars %>%
mutate(!! x := !!y)
Note that we do not use =
, because !!x = !!y
is not a syntactically valid code. We used the sym
function to transform a string into a symbol, so dplyr would look like a variable name.