3

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.

I want to merge both lists so that dataframes with same names (e.g. "$Q1$C") will be combined like with rbind, and new ones will be added to the list. 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. E.g.:

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

Any help is appreciated!

rosapluesch
  • 250
  • 2
  • 10

3 Answers3

5

Here is a solution using base 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 Q1s together and Q2s together;
  • xsub <- do.call(c, unname(i)) will combine Q1s or Q2s into a list data.frames;
  • split(xsub, names(xsub)) will group data.frames by their names (A, B, C);

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
mt1022
  • 16,834
  • 5
  • 48
  • 71
  • I like this solution without a library, too. Thanks! I was wondering if there is a one-line solution as I was trying, but yours seems to be an elegant way. Great! – rosapluesch Dec 06 '17 at 12:04
0

Here is an approach using 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)))
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
0

Using 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)
jjl
  • 326
  • 1
  • 5