Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding data frames as list elements (using for loop)

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?

like image 216
Riccardo Avatar asked Nov 05 '13 14:11

Riccardo


People also ask

How do you add a DataFrame to a list?

By using df. loc[index]=list you can append a list as a row to the DataFrame at a specified Index, In order to add at the end get the index of the last record using len(df) function. The below example adds the list ["Hyperion",27000,"60days",2000] to the end of the pandas DataFrame.

Can you have a list of data frames?

A Data frame is simply a List of a specified class called “data. frame”, but the components of the list must be vectors (numeric, character, logical), factors, matrices (numeric), lists, or even other data frames.

Can you add a list to a DataFrame in R?

If a list has the same length of elements (not sub-elements) as the length of each vector for which we want to create the data frame then we first need to create the data frame of vectors then we can easily add the list into the data frame.


2 Answers

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="")
like image 148
Christopher Louden Avatar answered Oct 22 '22 10:10

Christopher Louden


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.

like image 38
RickC Avatar answered Oct 22 '22 10:10

RickC