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 (eg "$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. Eg:
L3 <- Map('rbind',lapply(L1,'['),lapply(L2,'['))
Any help is appreciated!
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 Q1
s together and Q2
s together; xsub <- do.call(c, unname(i))
will combine Q1
s or Q2
s into a list data.frames
; split(xsub, names(xsub))
will group data.frame
s 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
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
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:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.