简体   繁体   English

更好地连接嵌套在R中的列表中的数据帧的方法

[英]Better way to join data frames nested in a list within R

I have a list object that contains nested lists, each includes a data frame. 我有一个包含嵌套列表的列表对象,每个列表都包含一个数据框。 The code below simulates my data structure: 下面的代码模拟了我的数据结构:

## simulate my data structure -- list of data frames
mylist <- list()
for (i in 1:5) {
 tmp <- list(data = data.frame(x=sample(1:5, replace=T), y=sample(6:10, replace=T)))
 mylist <- c(mylist, tmp)
}

I am looking to row bind all of my dataframes in order to create one master data frame. 我期待对所有数据帧进行行绑定,以便创建一个主数据帧。 Currently I use a for loop to complete this action: 目前我使用for循环来完成此操作:

## goal: better way to combine row bind data frames
## I like rbind.fill because sometimes my data are not as clean as desired
library(plyr)
df <- data.frame(stringsAsFactors=F)
for (i in 1:length(mylist)) {
 tmp <- mylist[i]$data
 df <- rbind.fill(df, tmp)
}

In reality, my master list is quite large - length of 3700, not 5 - so my for loop is quite slow. 实际上,我的主列表非常大 - 长度为3700,而不是5 - 所以我的for循环非常慢。

Is there a faster way to complete the same task? 有没有更快的方法来完成相同的任务?

ldply(mylist, data.frame)

# if you dont need the id column, 

ldply(mylist, data.frame)[,-1]

# If you want a progress bar for the larger operation, add .progress
ldply(mylist, data.frame, .progress = 'text')

# See ?create_progress_bar for more options.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM