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!