在 R 中将字符串识别为变量名 [英] Getting strings recognized as variable names in R

查看:79
本文介绍了在 R 中将字符串识别为变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关:R 中作为变量引用的字符串
可能相关:连接表达式以对数据帧进行子集化

我已根据评论请求简化了问题.这是一些示例数据.

I've simplified the question per the comment request. Here goes with some example data.

dat <- data.frame(num=1:10,sq=(1:10)^2,cu=(1:10)^3)
set1 <- subset(dat,num>5)
set2 <- subset(dat,num<=5)

现在,我想用这些来制作气泡图.我有一个更复杂的数据集,包含 3 种以上的颜色和复杂的子集,但我这样做了:

Now, I'd like to make a bubble plot from these. I have a more complicated data set with 3+ colors and complicated subsets, but I do something like this:

symbols(set1$sq,set1$cu,circles=set1$num,bg="red")
symbols(set2$sq,set2$cu,circles=set2$num,bg="blue",add=T)

我想做一个这样的 for 循环:

I'd like to do a for loop like this:

colors <- c("red","blue")
sets <- c("set1","set2")
vars <- c("sq","cu","num")

for (i in 1:length(sets)) {
   symbols(sets[[i]][,sq],sets[[i]][,cu],circles=sets[[i]][,num],
   bg=colors[[i]],add=T)
}    

我知道你可以评估一个变量来指定列(比如 var="cu"; set1[,var]; 我想知道如何获取一个变量来指定数据.框架本身(和另一个评估列).

I know you can have a variable evaluated to specify the column (like var="cu"; set1[,var]; I want to know how to get a variable to specify the data.frame itself (and another to evaluate the column).

更新:跑过这篇文章 r-bloggers 上有这个例子:

Update: Ran across this post on r-bloggers which has this example:

x <- 42
eval(parse(text = "x"))
[1] 42

我现在可以做这样的事情:

I'm able to do something like this now:

eval(parse(text=paste(set[[1]],"$",var1,sep="")))

在摆弄这个时,我发现有趣的是以下不等价:

In fiddling with this, I'm finding it interesting that the following are not equivalent:

vars <- data.frame("var1","var2")
eval(parse(text=paste(set[[1]],"$",var1,sep="")))
eval(parse(text=paste(set[[1]],"[,vars[[1]]]",sep="")))

我实际上必须这样做:

eval(parse(text=paste(set[[1]],"[,as.character(vars[[1]])]",sep="")))

Update2: 以上适用于输出值......但不是试图绘制.我做不到:

Update2: The above works to output values... but not in trying to plot. I can't do:

for (i in 1:length(set)) {
symbols(eval(parse(text=paste(set[[i]],"$",var1,sep=""))),
       eval(parse(text=paste(set[[i]],"$",var2,sep=""))),
       circles=paste(set[[i]],".","circles",sep=""),
       fg="white",bg=colors[[i]],add=T)
}

我得到无效的符号坐标.我检查了 set[[1]] 的类,这是一个因素.如果我这样做 is.numeric(as.numeric(set[[1]])) 我得到 TRUE.即使我在 eval 语句之前添加上面的内容,我仍然会收到错误消息.奇怪的是,我可以这样做:

I get invalid symbol coordinates. I checked the class of set[[1]] and it's a factor. If I do is.numeric(as.numeric(set[[1]])) I get TRUE. Even if I add that above prior to the eval statement, I still get the error. Oddly, though, I can do this:

set.xvars <- as.numeric(eval(parse(text=paste(set[[i]],"$",var1,sep=""))))
set.yvars <- as.numeric(eval(parse(text=paste(set[[i]],"$",var2,sep=""))))
symbols(xvars,yvars,circles=data$var3)

为什么作为变量存储与在符号函数中执行时的行为不同?

Why different behavior when stored as a variable vs. executed within the symbol function?

推荐答案

您找到了一个答案,即 eval(parse()) .您还可以研究 do.call(),这通常更易于实现.请记住有用的 as.name() 工具,用于将字符串转换为变量名称.

You found one answer, i.e. eval(parse()) . You can also investigate do.call() which is often simpler to implement. Keep in mind the useful as.name() tool as well, for converting strings to variable names.

这篇关于在 R 中将字符串识别为变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆