The best place to understand how to use strings instead of variable names is this document Programming with dplyr.
It is necessary to do something very similar to parse
and eval
that you quoted, but the dplyr
, provides through the package rlang
, a more intuitive way. It is worth reading also the website of rlang
.
Suppose you have two strings representing variable names:
x <- "nova_coluna"
y <- "hp"
You can create a new column called nova_coluna
which equals the variable hp
as follows:
library(dplyr)
library(rlang)
y <- sym(y)
mtcars %>%
mutate(!! x := !!y)
Note that we do not use =
, for !!x = !!y
is not a syntactically valid code. We use the function sym
to turn a string into a symbol, so dplyr would look like a variable name.
get("stringA")
works. As for"Newstring"
, I believe only by changing the name of the column a posteriori.– Rui Barradas