Convert string to a variable name

2019-01-01 08:26发布

问题:

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?

回答1:

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



回答2:

You can use do.call:

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


回答3:

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 <- 42
eval(parse(text = \"x\"))
[1] 42

And the opposite:

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


回答4:

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

I dunno if it answers your question correctly



回答5:

strsplit to parse your input and, as Greg mentioned, assign to assign the 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()


回答6:

assign is good, but I have not found a function for referring back to the variable you\'ve created in an automated script. (as.name seems to work the opposite way). More experienced coders will doubtless have a better solution, but this solution works and is slightly humorous perhaps, in that it gets R to write code for itself to execute.

Say I have just assigned value 5 to x (var.name <- \"x\"; assign(var.name, 5)) and I want to change the value to 6. If I am writing a script and don\'t know in advance what the variable name (var.name) will be (which seems to be the point of the assign function), I can\'t simply put x <- 6 because var.name might have been \"y\". So I do:

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 anything other than \"x\", that variable will similarly have been changed to 6.



回答7:

For some reason I cannot comment, only answer, so please, someone with the right, attach this as a comment to the last answer: The function you are looking for (for reffering to the value of variable which name is made automaticaly) is get(), at least it works for me:

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



回答8:

I was working with this a few days ago, and noticed that sometimes you will need to use the get() function to print the results of your variable. ie :

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 variable varnames[1], it returns \'jan\'. To work around this, you need to do print(get(varnames[1]))



标签: string r