Convert string to a variable name Convert string to a variable name r r

Convert string to a variable name


assign is what you are looking for.

assign("x", 5)x[1] 5

but buyer beware.

See R FAQ 7.21http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f


You can use do.call:

 do.call("<-",list(parameter_name, parameter_value))


There is another simple solution found there: http://www.r-bloggers.com/converting-a-string-to-a-variable-name-on-the-fly-and-vice-versa-in-r/

To convert a string to a variable:

x <- 42eval(parse(text = "x"))[1] 42

And the opposite:

x <- 42deparse(substitute(x))[1] "x"