将字符串转换为变量名 [英] Convert string to a variable name
本文介绍了将字符串转换为变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 R 来解析以下形式的字符串列表:
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"
然后,我想将该值分配给与 parameter_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?
推荐答案
assign 就是您要找的.p>
assign is what you are looking for.
assign("x", 5)
x
[1] 5
但买家要当心.
参见 R 常见问题 7.21http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f
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
这篇关于将字符串转换为变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文