How can I recursively concatenate the relevant list items
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 in which the first element is the same, but the first element of the second list is combined with the first list of element 1 and the second combined with seond and similarly for the third element of the list, the first data frame should be combined with the first two data frames of the previous list items and the second merge with the corresponding second two items.
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
That should do the trick. (I did not insert into the output, since it is so stretched, and easy enough to play on your own console.)
# Create some analogous data
df <- data.frame(ID=LETTERS[1:6], Values=5*(1:6), stringsAsFactors=FALSE)
DL <- list(list(`1`=df[1,], `2`=df[2,]),
list(`1`=df[3,], `2`=df[4,]),
list(`1`=df[5,], `2`=df[6,]))
# Build a function that does what you want for a pair of inputs
myrbind <- function(x, y) {
mapply(rbind, y, x, SIMPLIFY=FALSE)
}
myrbind(DL[[1]], DL[[2]]) # Try it out
# Use Reduce to make the merges accumulate as it works through the list.
Reduce(f = myrbind, x = DL, accumulate = TRUE)