开发者

Recombining a list of Data.frames into a single data frame [duplicate]

This question already has answers here: Combine a list of data frames into one data frame by row (9 answers) Closed 5 years ago.

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


Maybe you want to do something like:

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


dplyr lets you use bind_rows function for that:

library(dplyr)

foo <- list(df1 = data.frame(x=c('a', 'b', 'c'),y = c(1,2,3)), 
         df2 = data.frame(x=c('d', 'e', 'f'),y = c(4,5,6)))

bind_rows(foo)


Note that the basic solution

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

will be slow if we have many dataframes. A scalable solution is:

library(data.table)
rbindlist(my.df.list)

which, from the docs, is the same as do.call("rbind", l) on data.frames, but much faster.


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜