list 将 Data.frames 列表重组为单个数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2392915/
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
Recombining a list of Data.frames into a single data frame
提问by CGN
I am sorry if this question has been answered already. Also, this is my first time on stackoverflow.
如果已经回答了这个问题,我很抱歉。此外,这是我第一次使用 stackoverflow。
I have a beginner R question concerning lists , data frames and merge()
and/or rbind()
.
我有一个关于列表、数据框merge()
和/或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.
为了提高效率,我创建了一个列表,其中包含每个国家/地区的一个数据框,并对每个人进行了各种计算data.frame
。但是,我似乎无法再次将单个数据帧合并为一个大帧。
rbind()
and merge()
both tell me that only replacement of elements is allowed.
rbind()
并且merge()
都告诉我只允许替换元素。
Could someone tell me what I am doing wrong/ and how to actually recombine the data frames?
有人能告诉我我做错了什么/以及如何实际重新组合数据帧吗?
Thank you
谢谢
回答by datanalytics.com
Maybe you want to do something like:
也许你想做这样的事情:
do.call("rbind", my.df.list)
do.call("rbind", my.df.list)
回答by lbcommer
dplyr lets you use bind_rows function for that:
dplyr 允许您使用 bind_rows 函数:
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)
回答by alberto
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.
其中,从文档中,是same as do.call("rbind", l) on data.frames, but much faster.
回答by doug
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
回答by Shane
plyr is probably best. Another useful approach if the data frames can be different is to use reshape:
plyr 可能是最好的。如果数据框可以不同,另一种有用的方法是使用 reshape:
library(reshape)
data <- merge_recurse(listofdataframes)
Look at my answer to this related question on merging data frames.
看看我对这个有关合并数据框的相关问题的回答。