rbind dataframes across nested lists

不羁岁月 提交于 2020-07-18 11:38:29

问题


I've had a look at various rbinding list questions such as this but I can't really find a more efficient way of doing this.

I have a nested list nestlist that contains three lists which each contain two dataframes:

df1 <- data.frame(ID = paste0(LETTERS[1:4],1:4), valueA = seq(0.1,0.4,0.1), Category= "Apples")
df2 <- data.frame(ID = paste0(LETTERS[1:4],1:4), valueB = seq(0.1,0.4,0.1),  Category= "Apples")
list1 <- list(df1,df2)

df3 <- data.frame(ID = paste0(LETTERS[1:4],1:4), valueA = seq(0.1,0.4,0.1), Category= "Pears")
df4 <- data.frame(ID = paste0(LETTERS[1:4],1:4), valueB = seq(0.1,0.4,0.1),  Category= "Pears")
list2 <- list(df3,df4)

df5 <- data.frame(ID = paste0(LETTERS[1:4],1:4), valueA = seq(0.1,0.4,0.1), Category= "Stairs")
df6 <- data.frame(ID = paste0(LETTERS[1:4],1:4), valueB = seq(0.1,0.4,0.1),  Category= "Stairs")
list3 <- list(df5,df6)

nestedlist <- list(list1,list2,list3)

I want to find an easier way to rbind each object from list1, list2 and list 3 by the common value column so that I end up with:

rbind(nestedlist[[1]][[1]],nestedlist[[2]][[1]], nestedlist[[3]][[1]])

  ID   A Category
1  A1 0.1   Apples
2  B2 0.2   Apples
3  C3 0.3   Apples
4  D4 0.4   Apples
5  A1 0.1    Pears
6  B2 0.2    Pears
7  C3 0.3    Pears
8  D4 0.4    Pears
9  A1 0.1   Stairs
10 B2 0.2   Stairs
11 C3 0.3   Stairs
12 D4 0.4   Stairs

回答1:


You can use do.call(Map, ...), this passes the nested lists as arguments to Map which will loop through these lists in a parallel way and call rbind as the Map function will bind lists at the same positions together:

do.call(Map, c(f = rbind, nestedlist))

# [[1]]
#    ID valueA Category
# 1  A1    0.1   Apples
# 2  B2    0.2   Apples
# 3  C3    0.3   Apples
# 4  D4    0.4   Apples
# 5  A1    0.1    Pears
# 6  B2    0.2    Pears
# 7  C3    0.3    Pears
# 8  D4    0.4    Pears
# 9  A1    0.1   Stairs
# 10 B2    0.2   Stairs
# 11 C3    0.3   Stairs
# 12 D4    0.4   Stairs
# 
# [[2]]
#    ID valueB Category
# 1  A1    0.1   Apples
# 2  B2    0.2   Apples
# 3  C3    0.3   Apples
# 4  D4    0.4   Apples
# 5  A1    0.1    Pears
# 6  B2    0.2    Pears
# 7  C3    0.3    Pears
# 8  D4    0.4    Pears
# 9  A1    0.1   Stairs
# 10 B2    0.2   Stairs
# 11 C3    0.3   Stairs
# 12 D4    0.4   Stairs



回答2:


We can try

library(purrr)
lapply(transpose(nestedlist), function(x) do.call(rbind, x))

Or use bind_rows from dplyr

library(dplyr)
transpose(nestedlist) %>% 
                 map(bind_rows)
#[[1]]
#   ID valueA Category
#1  A1    0.1   Apples
#2  B2    0.2   Apples
#3  C3    0.3   Apples
#4  D4    0.4   Apples
#5  A1    0.1    Pears
#6  B2    0.2    Pears
#7  C3    0.3    Pears
#8  D4    0.4    Pears
#9  A1    0.1   Stairs
#10 B2    0.2   Stairs
#11 C3    0.3   Stairs
#12 D4    0.4   Stairs

#[[2]]
#   ID valueB Category
#1  A1    0.1   Apples
#2  B2    0.2   Apples
#3  C3    0.3   Apples
#4  D4    0.4   Apples
#5  A1    0.1    Pears
#6  B2    0.2    Pears
#7  C3    0.3    Pears
#8  D4    0.4    Pears
#9  A1    0.1   Stairs
#10 B2    0.2   Stairs
#11 C3    0.3   Stairs
#12 D4    0.4   Stairs


来源:https://stackoverflow.com/questions/41596326/rbind-dataframes-across-nested-lists

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!