19

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.

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.

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 (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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Riccardo
  • 743
  • 2
  • 5
  • 14
  • 5
    `list <- mget( ls( pattern = "^EOG20[0-1][0-9]$" ) )` See e.g. this answer for more info http://stackoverflow.com/a/17307236/1478381 on `mget` or the [help page](http://stat.ethz.ch/R-manual/R-devel/library/base/html/get.html) – Simon O'Hanlon Nov 05 '13 at 14:53
  • 1
    Thank you SimonO101. This solution is probably the most efficient one. – Riccardo Nov 05 '13 at 15:10

3 Answers3

24

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

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 j directly in the paste functions:

sEOG <- paste("EOG", j, sep="")
Christopher Louden
  • 7,540
  • 2
  • 26
  • 29
22

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:

 > 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.

RickC
  • 260
  • 3
  • 9
1

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:

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
llrs
  • 3,308
  • 35
  • 68