Search code examples
stringr

Convert string to a variable name


I am using R to parse a list of strings in the form:

original_string <- "variable_name=variable_value"

First, I extract the variable name and value from the original string and convert the value to numeric class.

parameter_value <- as.numeric("variable_value")
parameter_name <- "variable_name"

Then, I would like to assign the value to a variable with the same name as the parameter_name string.

variable_name <- parameter_value

What is/are the function(s) for doing this?


Solution

  • assign is what you are looking for.

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

    but buyer beware.

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