list 添加数据框作为列表元素(使用 for 循环)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19791760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 02:06:50  来源:igfitidea点击:

Adding data frames as list elements (using for loop)

rlistfor-loopdataframe

提问by Riccardo

I have in my environment a series of data frames called EOG. There is one for each year between 2006 and 2012. Like, EOG2006, EOG2007...EOG2012. I would like to add them as elements of a list.

我的环境中有一系列名为 EOG 的数据框。2006 年到 2012 年之间每年都有一个。例如,EOG2006、EOG2007...EOG2012。我想将它们添加为列表的元素。

First, I am trying to know if this is possible. I read the official R guide and a couple of R programming manuals but I didn't find explicit examples about that.

首先,我想知道这是否可能。我阅读了官方 R 指南和一些 R 编程手册,但我没有找到明确的例子。

Second, I would like to do this using a for loop. Unfortunately, the code I used to do the job is wrong and I am going crazy to fix it.

其次,我想使用 for 循环来做到这一点。不幸的是,我用来做这项工作的代码是错误的,我要疯狂地修复它。

for (j in 2006:2012){
z<-j
sEOG<-paste("EOG", z, sep="")
dEOG<-get(paste("EOG", z, sep=""))
lsEOG<-list()
lsEOG[[sEOG]]<-dEOG
}

This returns a list with one single element. Where is the mistake?

这将返回一个包含一个元素的列表。错误在哪里?

回答by Christopher Louden

You keep reinitializing the list inside the loop. You need to move lsEOG<-list()outside the forloop.

您不断重新初始化循环内的列表。您需要将lsEOG<-list()for循环。

lsEOG<-list()

for (j in 2006:2012){
  z <- j
  sEOG <- paste("EOG", z, sep="")
  dEOG <- get(paste("EOG", z, sep=""))
  lsEOG[[sEOG]] <-dEOG
}

Also, you can use jdirectly in the pastefunctions:

此外,您可以j直接在paste函数中使用:

sEOG <- paste("EOG", j, sep="")

回答by RickC

I had the same question, but felt that the OP's initial code was a bit opaque for R beginners. So, here is perhaps a bit clearer example of how to create data frames in a loop and add them to a list which I just now figured out by playing around in the R shell:

我有同样的问题,但觉得 OP 的初始代码对于 R 初学者来说有点不透明。因此,这里可能是一个更清晰的示例,说明如何在循环中创建数据帧并将它们添加到我刚刚通过在 R shell 中玩弄的列表中:

 > dfList <- list()  ## create empty list
 >
 > for ( i in 1:5 ) {
 +     x <- rnorm( 4 )
 +     y <- sin( x )
 +     dfList[[i]] <- data.frame( x, y )  ## create and add new data frame
 + }
 >
 > length( dfList )  ## 5 data frames in list
 [1] 5
 >
 > dfList[[1]]    ## print 1st data frame
            x          y
 1 -0.3782376 -0.3692832
 2 -1.3581489 -0.9774756
 3  1.2175467  0.9382535
 4 -0.7544750 -0.6849062
 >
 > dfList[[2]]    ## print 2nd data frame
            x          y
 1 -0.1211670 -0.1208707
 2 -1.5318212 -0.9992406
 3  0.8790863  0.7701564
 4  1.4014124  0.9856888
 >
 > dfList[[2]][4,2]   ## in 2nd data frame, print element in row 4 column 2
 [1] 0.9856888
 >

For R beginners like me, note that double brackets are required to access the ith data frame. Basically, double brackets are used for lists while single brackets are used for vectors.

对于像我这样的 R 初学者,请注意访问第 i 个数据框需要双括号。基本上,双括号用于列表,而单括号用于向量。

回答by llrs

If the data frames are saved as an object you can find them by apropos("EOG", ignore.case=FALSE)and them with a loop store them in the list:

如果数据框被保存为一个对象,你可以通过apropos("EOG", ignore.case=FALSE)循环找到它们,并将它们存储在列表中:

list.EOG<- apropos("EOG", ignore.case=FALSE) #Find the objects with case sensitive 
lsEOG<-NULL #Creates the object to full fill in the list
for (j in 1:length(list.EOG)){
lsEOG[i]<-get(list.EOG[i]) #Add the data.frame to each element of the list
}

to add the name of each one to the list you can use:

要将每个人的名称添加到您可以使用的列表中:

names(lsEOG, "names")<-list.EOG