访问变量值,其中变量名称存储在字符串中
但我正在尝试这样做 我有
:
ret_series <- c(1, 2, 3)
x <- "ret_series"
如何通过调用 x
上的某些函数/操作来获得 (1, 2, 3)
,而不直接提及 ret_series< /代码>?
Similar questions have been raised for other languages: C, sql, java, etc.
But I'm trying to do this in R.
I have:
ret_series <- c(1, 2, 3)
x <- "ret_series"
How do I get (1, 2, 3)
by calling some function / manipulation on x
, without direct mentioning of ret_series
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您在问题中提供了答案。尝试
获取
。You provided the answer in your question. Try
get
.对于一次性使用, get 函数可以工作(如前所述),但它不能很好地扩展到更大的项目。最好将数据存储在列表或环境中,然后使用 [[ 访问各个元素:
For a one off use, the get function works (as has been mentioned), but it does not scale well to larger projects. it is better to store you data in lists or environments, then use [[ to access the individual elements:
以下任一情况有什么问题?
What's wrong with either of the following?
请注意,上面的一些示例不适用于
data.frame
。例如,给定
x <- data.frame(a=seq(1,5))
get("x$a")
不会给你x$a
。Note that some of the examples above wouldn't work for a
data.frame
.For instance, given
x <- data.frame(a=seq(1,5))
get("x$a")
would not give youx$a
.