从许多对象的列表中提取数据框 [英] extracting a dataframe from a list over many objects
问题描述
我在R中有1000多个对象( z
),每个对象包含三个数据帧( df1
, df2
, df3
)具有不同的结构.
I have over a 1000 objects (z
) in R, each containing three dataframes (df1
, df2
, df3
) with different structures.
z1 $ df1
……z1000 $ df1
z1 $ df2
… z1000 $ df2
z1 $ df3
… z1000 $ df3
我创建了这些对象的列表(因此list1包含z1到z1000),并尝试使用 lapply
为所有对象提取一种类型的数据帧( df2
),然后将它们合并到一个数据框.
I created a list of these objects (list1 thus contains z1 thru z1000) and tried to use lapply
to extract one type of dataframe (df2
) for all objects, and then merge them to one single dataframe.
对于单个对象,它看起来像这样:
For a single object it would look like this:
df15<- z15$df2 # I transferred the index of z to the extracted df
我尝试使用 lapply
进行了一些编码,而忽略了索引的传输(我可以为此创建另一个列表).但是我不知道应该使用什么功能.
I tried some code with lapply
, ignoring the transfer of the index (I can create another list for that). However I don’t know what function I should use.
List2 <- lapply(list1, function(x))
我尝试避免使用循环,因为循环太多了,向量化也快得多.我的想法是我从错误的角度看待它.
I try to avoid using a loop because there's so many and vectorization is so much quicker. I have the idea I'm looking at it from the wrong angle.
后续合并可以如下进行:
Subsequent merging can be done as follows:
merged <- do.call(rbind, list2)
谢谢您的建议.
推荐答案
一个选项可能是使用 lapply
提取 data.frame
,然后使用 bind_rows
来自 dplyr
.
One option could be using lapply
to extract data.frame
and then use bind_rows
from dplyr
.
## The data
df1 <- data.frame(id = c(1:10), name = c(LETTERS[1:10]), stringsAsFactors = FALSE)
df2 <- data.frame(id = 11:20, name = LETTERS[11:20], stringsAsFactors = FALSE)
df3 <- data.frame(id = 21:30, name = LETTERS[15:24], stringsAsFactors = FALSE)
df4 <- data.frame(id = 121:130, name = LETTERS[15:24], stringsAsFactors = FALSE)
z1 <- list(df1 = df1, df2 = df2, df3 = df3)
z2 <- list(df1 = df1, df2 = df2, df3 = df3)
z3 <- list(df1 = df1, df2 = df2, df3 = df3)
z4 <- list(df1 = df1, df2 = df2, df3 = df4) #DFs can contain different data
# z <- list(z1, z2, z3, z4)
# Dynamically populate list z with many list object
z <- as.list(mget(paste("z",1:4,sep="")))
df1_all <- bind_rows(lapply(z, function(x) x$df1))
df2_all <- bind_rows(lapply(z, function(x) x$df2))
df3_all <- bind_rows(lapply(z, function(x) x$df3))
## Result for df3_all
> tail(df3_all)
## id name
## 35 125 S
## 36 126 T
## 37 127 U
## 38 128 V
## 39 129 W
## 40 130 X
这篇关于从许多对象的列表中提取数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!