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
This should do the trick. (I've not pasted in the output since it's so sprawling, and is easy enough to reproduce 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)
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.