简体   繁体   English

R:合并两个数据帧列表

[英]R: merge two lists of lists of dataframes

I have two lists of lists of dataframes like this: 我有两个数据帧列表,如下所示:

L1 <- list(Q1=list(A=data.frame(X1=1:3),C=data.frame(X1=1:3)),
        Q2=list(B=data.frame(X1=1:3),C=data.frame(X1=1:3)))
L2 <- list(Q1=list(B=data.frame(X1=4:6),C=data.frame(X1=4:6)),
        Q2=list(A=data.frame(X1=4:6),C=data.frame(X1=4:6)))

The names on the first level "Q1" and "Q2" are identical in both lists. 第一级“Q1”和“Q2”的名称在两个列表中都相同。

I want to merge both lists so that dataframes with same names (eg "$Q1$C") will be combined like with rbind , and new ones will be added to the list. 我想合并两个列表,以便将具有相同名称的数据帧(例如“$ Q1 $ C”)与rbind组合在一起,并将新的列表添加到列表中。 The desired output should look like this: 所需的输出应如下所示:

> L3
$Q1
$Q1$A
  X1
1  1
2  2
3  3

$Q1$B
  X1
1  4
2  5
3  6

$Q1$C
  X1
1  1
2  2
3  3
4  4
5  5
6  6


$Q2
$Q2$A
  X1
1  4
2  5
3  6

$Q2$B
  X1
1  1
2  2
3  3

$Q2$C
  X1
1  1
2  2
3  3
4  4
5  5
6  6

I tried some combinations using Map() and lapply() but I could not fix it, yet. 我尝试了一些使用Map()lapply()但我无法修复它。 Eg: 例如:

L3 <- Map('rbind',lapply(L1,'['),lapply(L2,'['))

Any help is appreciated! 任何帮助表示赞赏!

Here is a solution using base R: 这是一个使用基数R的解决方案:

x <- c(L1, L2)
lapply(split(x, names(x)), function(i){
    xsub <- do.call(c, unname(i))
    lapply(split(xsub, names(xsub)), function(j) do.call(rbind, unname(j)))
})
  • split(x, names(x)) will put Q1 s together and Q2 s together; split(x, names(x))Q1组合在一起, Q2组合在一起;
  • xsub <- do.call(c, unname(i)) will combine Q1 s or Q2 s into a list data.frames ; xsub <- do.call(c, unname(i))Q1Q2组合成一个列表data.frames ;
  • split(xsub, names(xsub)) will group data.frame s by their names ( A , B , C ); split(xsub, names(xsub))将按名称( ABC )对data.frame进行分组;

The output is: 输出是:

# $Q1
# $Q1$A
# X1
# 1  1
# 2  2
# 3  3
# 
# $Q1$B
# X1
# 1  4
# 2  5
# 3  6
# 
# $Q1$C
# X1
# 1  1
# 2  2
# 3  3
# 4  4
# 5  5
# 6  6
# 
# 
# $Q2
# $Q2$A
# X1
# 1  4
# 2  5
# 3  6
# 
# $Q2$B
# X1
# 1  1
# 2  2
# 3  3
# 
# $Q2$C
# X1
# 1  1
# 2  2
# 3  3
# 4  4
# 5  5
# 6  6

Here is an approach using reshape2::melt . 这是一个使用reshape2::melt的方法。

library(reshape2);

# Collapse lists and turn into long dataframe
df.long <- rbind.data.frame(
    melt(L1, id.vars = "X1"),
    melt(L2, id.vars = "X1"));

# Split dataframe into nested list
lst <- lapply(split(df.long, df.long$L1), function(x) split(x, x$L2));
lst <- lapply(lst, function(x) lapply(x, function(y) data.frame(X1 = y$X1)));

str(lst);
#List of 2
# $ Q1:List of 3
#  ..$ A:'data.frame':  3 obs. of  1 variable:
#  .. ..$ X1: int [1:3] 1 2 3
#  ..$ B:'data.frame':  3 obs. of  1 variable:
#  .. ..$ X1: int [1:3] 4 5 6
#  ..$ C:'data.frame':  6 obs. of  1 variable:
#  .. ..$ X1: int [1:6] 1 2 3 4 5 6
# $ Q2:List of 3
#  ..$ A:'data.frame':  3 obs. of  1 variable:
#  .. ..$ X1: int [1:3] 4 5 6
#  ..$ B:'data.frame':  3 obs. of  1 variable:
#  .. ..$ X1: int [1:3] 1 2 3
#  ..$ C:'data.frame':  6 obs. of  1 variable:
#  .. ..$ X1: int [1:6] 1 2 3 4 5 6

Data 数据

L1 <- list(Q1=list(A=data.frame(X1=1:3),C=data.frame(X1=1:3)),
        Q2=list(B=data.frame(X1=1:3),C=data.frame(X1=1:3)))
L2 <- list(Q1=list(B=data.frame(X1=4:6),C=data.frame(X1=4:6)),
        Q2=list(A=data.frame(X1=4:6),C=data.frame(X1=4:6)))

Using purrr: 使用purrr:

library(tidyverse)

f <- function(x) {
  map_df(map(x, bind_rows, .id = "id1"), bind_rows, .id = "id2")
}

list(L1, L2) %>%
  map_df(f) %>%
  split(list(.$id1, .$id2)) %>%
  map(select, X1)

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

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