Skip to Content

How can I merge recursively the corresponding elements of a List of Lists

Example

a
[[1]]
[[1]]$`1`
    ID     Values
1   4     160.08858
2   8     83.35774
3   30    51.21873
4   38    54.92554
5   44    77.06082
 
[[1]]$`2`
    ID     Values
1   4     0.08858
2   8     183.35774
 
[[2]]
[[2]]$`1`
    ID     Values
3   30    51.21873
4   38    54.92554
5   44    77.06082
 
[[2]]$`2`
    ID     Values
1   4     0.08858
2   8     183.35774
3   30    51.21873
5   44    77.06082
 
[[3]]
[[3]]$`1`
    ID     Values
5   44    77.06082
 
[[3]]$`2`
    ID     Values
1   4     0.08858
3   30    51.21873

I would like to have a list where the first element is the same but the first list's second element is merged with the first list of element 1 and second merged with seond and simillarly for the third element of the list the first dataframe should be merged with the first two data frames of the previous elements of the list and second mergerd with the corresponding second two elements.

RESULT should be

RESULT
[[1]]
[[1]]$`1`
    ID     Values
1   4     160.08858
2   8     83.35774
3   30    51.21873
4   38    54.92554
5   44    77.06082
 
[[1]]$`2`
    ID     Values
1   4     0.08858
2   8     183.35774
 
[[2]]
[[2]]$`1`
    ID     Values
3   30    51.21873
4   38    54.92554
5   44    77.06082
1   4     160.08858
2   8     83.35774
3   30    51.21873
4   38    54.92554
5   44    77.06082
 
 
[[2]]$`2`
    ID     Values
1   4     0.08858
2   8     183.35774
3   30    51.21873
5   44    77.06082
1   4     0.08858
2   8     183.35774
 
 
[[3]]
[[3]]$`1`
    ID     Values
5   44    77.06082
3   30    51.21873
4   38    54.92554
5   44    77.06082
1   4     160.08858
2   8     83.35774
3   30    51.21873
4   38    54.92554
5   44    77.06082
 
 
[[3]]$`2`
    ID     Values
1   4     0.08858
3   30    51.21873
1   4     0.08858
2   8     183.35774
3   30    51.21873
5   44    77.06082
1   4     0.08858
2   8     183.35774