2014-02-05 79 views
1

我基本上希望能够使用循环的past()和i(j ..)值从for循环(实际上是两个嵌套for循环)中调用列以灵活的方式明智地访问我的数据帧列 。在R中调用for循环的列

#for the showcase I use the standard cars example 
r1 <- cars 
r2 <- cars 

# in case there are more data to consider I would want to add, ore remove further with out changing the rest 


# here I am entering the "dimension" of what I want to compare for the showcase its only one 
num_r <- 2 #total number of reactors in the experiment 



    for(i in 1:num_r) 
    { 
    # shoud create proxie variable to be processed further 
    assign(paste("proxi_r",i,sep="", colapse="") , do.call("matrix",  
      list(get(paste("r",i,"$speed",sep="", colapse=""))))) 
    # further operations of gluing and arranging data follow so they fit tests formatting requirements 

    } 

这给了我:

Error in get(paste("r", i, "$speed", sep = "", colapse = "")) : 
object 'r1$speed' not found 

但是当典型R1 $速度它显然存在?我搜索了“R对象不存在于循环内”,“使用paste()来访问循环内的变量”,“foor循环和对象”,“do.call内部循环”....以及类似的内容。

有没有什么可以绕过get(),所以我不必研究环境主题,所以我可以保持我的循环的灵活性,所以我没有重新编辑我的脚本,每次我改变了实验配置,这是非常耗时的,并且允许大量的错误潜入其中。

由于大量使用excel宏,实验室中的每个人都在使用几次,所以数据的大小已经崩溃,所以没有回到convort区域。 我现在正在尝试用R静态图书以及大量的谷歌搜索和阅读教程深入研究R编程,所以请原谅我的幼稚方法和我糟糕的英语。 我会非常感谢任何提示,因为我觉得现在有点卡住了。

回答

1

这是一个普遍的混乱。您创建了一个对象名称“r1 $ speed”,即完整的字符串。这与由$speed子集化的对象r1不同。

尝试使用get(paste('r',i,collapse='',sep=''))$speed