tags:

views:

249

answers:

4

Hello everyone

I am sorry if this question has been answered already. Also, this is my first time on stackoverflow.

I have a beginner R question concerning lists , data frames and merge and/or rbind.

I started with a Panel that looks like this

COUNTRY YEAR VAR
A         1
A         2
B         1
B         2

For efficiency purposes, I created a list that consists of one data frame for each country and performed a variety of calculations on each individual data.frame. However, I cannot seem to combine the individual data frames into one large frame again.

rbind and merge both tell me that only replacement of elements is allowed.

Could someone tell me what I am doing wrong/ and how to actually recombine the data frames?

Thank you

+2  A: 

Hi there,

I suggest you to go and study the plyr package. (Here is a nice little tutorial for plyr. Here is a longer tutorial to read)

Then gave a look at the ldply function.

Tal Galili
Great! That did the trick. Thank you very much.
CG Nguyen
+2  A: 

There might be a better way to do this, but this seems to work and it's straightforward. (My code has four lines so that it's easier to see the steps; these four could easily be combined.)

# first re-create your data frame:
A = matrix( ceiling(10*runif(8)), nrow=4)
colnames(A) = c("country", "year_var")
dfa = data.frame(A)

# now re-create the list you made from the individual rows of the data frame:
df1 = dfa[1,]
df2 = dfa[2,]
df3 = dfa[3,]
df4 = dfa[4,]
df_all = list(df1, df2, df3, df4)

# to recreate your original data frame:
x = unlist(df_all)         # from your list create a single 1D array 
A = matrix(x, nrow=4)      # dimension that array in accord w/ your original data frame
colnames(A) = c("country", "year_var")     # put the column names back on
dfa = data.frame(A)        # from the matrix, create your original data frame
doug
Thank you for the script. It worked quite well, my only worry with this is that it doesn't automatically update if I were to add a country. (although I suppose with a for-loop I could do that, too)
CG Nguyen
+3  A: 

Hello,

Maybe you want to do something like

do.call( rbind, my.df.list )

datanalytics.com
Unfortunately, this returns an error (I think because not all panels are balanced?) Either way, the above command worked. Thank you though.
CG Nguyen
+2  A: 

plyr is probably best. Another useful approach if the data frames can be different is to use reshape:

library(reshape)
data <- merge_recurse(listofdataframes)

Look at my answer to this related question on merging data frames.

Shane
Thank you for the pointer. Will read up on that.
CG Nguyen