从R中的for循环内部调用列

从R中的for循环内部调用列

本文介绍了从R中的for循环内部调用列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I basically want to be capable to call columns from inside a for loop (in reality two nested for loops), using past() and i (j..) value of the loop to accessmy data frames columns wise in a flexible manner.

#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 $ speed时,它显然存在?

but when typ r1$speed it obviously exists??

Sofare我搜索了"R对象在循环内不存在",使用paste()在循环内访问变量","foor循环和对象","do.call循环内" ...等等.

Sofare I searched "R object dont exist inside loop", "using paste() to acces variables inside loop", "foor loops and objects","do.call inside loops" ....and similar...

有没有什么可以规避get()的,所以我不必研究环境的主题,因此我可以保持循环的灵活性,因此不必每次都重新编辑脚本时就可以更改了实验配置,这确实很耗时,并且允许许多错误潜入内部.

Is there anything to circumvent get() so I don’t have to look into the topic of environments, so I can keep the flexibility of my loops so I don’t have re-edit my script every time I have a changed the experimental configuration, which is really time consuming and allows a lot of errors to sneak inside.

由于大量使用excel宏,数据的大小使excel崩溃了,这里实验中的每个人都在使用excel宏:),因此没有返回到convort区域的信息.我现在正尝试通过一本R静态书籍以及大量的Google搜索和阅读教程来深入研究R编程,因此请原谅我的幼稚方法和糟糕的英语.我将非常感谢您提供的任何提示,因为我现在有点被卡住了.

The size of the data have crashed excel with extensive use of excel macros, which everyone in the lab here is using, several times :) , so there is no going back to the convort zone.I am now trying to dig into R programming with a R statics book, and a lot of googling and reading tutorials, so please forgive my naive approach, and my lousy English.I would be very thankful for any tips, as I feel sort of stuck right now.

推荐答案

这是常见的混淆.您已经创建了一个对象名称"r1 $ speed",即完整的字符串.这与由 $ speed 子集化的对象 r1 不同.

This is a common confusion. You've created an object name "r1$speed" , i.e. a complete character string. This is not the same as the object r1 subsetted by $speed .

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

这篇关于从R中的for循环内部调用列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:36