跨嵌套列表的 rbind 数据帧
rbind dataframes across nested lists
我看过各种 rbinding 列表问题,例如 this,但我真的找不到更有效的方法。
我有一个嵌套列表 nestlist
,其中包含三个列表,每个列表包含两个数据框:
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)
我想找到一种更简单的方法,通过公共 value
列对列表 1、列表 2 和列表 3 中的每个对象进行 rbind,这样我最终得到:
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
您可以使用 do.call(Map, ...)
,这会将嵌套列表作为参数传递给 Map,Map 将以并行方式循环遍历这些列表并调用 rbind
,因为 Map
函数将绑定同一位置一起列出:
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
我们可以试试
library(purrr)
lapply(transpose(nestedlist), function(x) do.call(rbind, x))
或使用 dplyr
中的 bind_rows
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
我看过各种 rbinding 列表问题,例如 this,但我真的找不到更有效的方法。
我有一个嵌套列表 nestlist
,其中包含三个列表,每个列表包含两个数据框:
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)
我想找到一种更简单的方法,通过公共 value
列对列表 1、列表 2 和列表 3 中的每个对象进行 rbind,这样我最终得到:
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
您可以使用 do.call(Map, ...)
,这会将嵌套列表作为参数传递给 Map,Map 将以并行方式循环遍历这些列表并调用 rbind
,因为 Map
函数将绑定同一位置一起列出:
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
我们可以试试
library(purrr)
lapply(transpose(nestedlist), function(x) do.call(rbind, x))
或使用 dplyr
bind_rows
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