Convert string to variable name

I use 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 source string and convert the value to a number class.

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

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

 variable_name <- parameter_value 

What is this function? (?)?

+88
string r
May 17 '11 at 17:22
source share
9 answers

Appointment 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

+110
May 17 '11 at 17:27
source share

You can use do.call:

  do.call("<-",list(parameter_name, parameter_value)) 
+59
May 17 '11 at 21:03
source share

There is another simple solution: 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 <- 42 eval(parse(text = "x")) [1] 42 

And vice versa:

 x <- 42 deparse(substitute(x)) [1] "x" 
+44
Jan 21 '16 at 15:24
source share

use x = as.name ("string") you can use, then use x to refer to a variable with a name string.

I do not know if he answered your question correctly.

+15
May 23 '13 at
source share

The function you are looking for is get() :

 assign ("abc",5) get("abc") 

Confirming that the memory address is identical:

 getabc <- get("abc") pryr::address(abc) == pryr::address(getabc) # [1] TRUE 

Link: R FAQ 7.21 How can I turn a string into a variable?

+12
May 7 '18 at 7:19
source share

strsplit to strsplit your input and, as Greg said, assign to assign variables.

 original_string <- c("x=123", "y=456") pairs <- strsplit(original_string, "=") lapply(pairs, function(x) assign(x[1], as.numeric(x[2]), envir = globalenv())) ls() 
+10
May 17 '11 at 17:41
source share

assign is good, but I did not find a function to go back to the variable that you created in the automatic script. ( as.name seems to work the other way around). More experienced coders will undoubtedly have a better solution, but this solution works and seems to be a bit humorous as it allows R to write code for itself.

Let's say I just assigned the value 5 to x ( var.name <- "x"; assign(var.name, 5) ), and I want to change the value to 6. If I write a script and don’t know in advance what variable name ( var.name ) will be (which seems to be the point of the assign function), I can't just put x <- 6 , because var.name could be "y" . Therefore I:

 var.name <- "x" #some other code... assign(var.name, 5) #some more code... #write a script file (1 line in this case) that works with whatever variable name write(paste0(var.name, " <- 6"), "tmp.R") #source that script file source("tmp.R") #remove the script file for tidiness file.remove("tmp.R") 

x will be changed to 6, and if the variable name was something other than "x" , this variable will be similarly changed to 6.

+5
Jul 14 '15 at 15:54
source share

I worked with this a few days ago and noticed that sometimes you need to use the get() function to print the results of your variable. those.:

 varnames = c('jan', 'feb', 'march') file_names = list_files('path to multiple csv files saved on drive') assign(varnames[1], read.csv(file_names[1]) # This will assign the variable 

From there, if you try to print the varnames[1] variables, it will return "jan". To get around this, you need to do print(get(varnames[1]))

+2
Jul 12 '18 at 18:29
source share

If you want to convert a string to a variable inside the function body, but want to have a global variable:

 test <- function() { do.call("<<-",list("vartest","xxx")) } test() vartest [1] "xxx" 
0
Jul 08 '19 at 7:04
source share



All Articles