【发布时间】:2015-12-02 20:45:43
【问题描述】:
为什么 get() 与 paste() 结合使用可用于数据帧,但不能用于数据帧中的列?我怎样才能让它工作?
ab<-12
get(paste("a","b",sep=""))
# gives: [1] 12
ab<-data.frame(a=1:3,b=3:5)
ab$a
#gives: [1] 1 2 3
get(paste("a","b",sep=""))
# gives the whole dataframe
get(paste("ab$","a",sep=""))
# gives: Error in get(paste("ab$", "a", sep = "")) : object 'ab$a' not found
【问题讨论】:
-
严肃的问题:你为什么要这样做?
-
你可以用
eval(parse(text = ...))来做这件事,但是R让这件事变得尴尬和困难的程度应该表明你使用get的整个方法是可能并不理想。 -
只是因为
$是一个函数,而get不会评估传递给它的对象,请尝试使用c:get("c(1,2,3)")