Home > Net >  `rbind` elements with same index in a list
`rbind` elements with same index in a list

Time:02-26

I have a list which has three lists each of which have 2 data.frames like following:-

d <- list(list(data.frame(height = rnorm(10), weight = runif(10)), data.frame(nr = rnorm(10), qr = rchisq(10, 10))),
          list(data.frame(height = rnorm(6), weight = runif(6)), data.frame(nr = rnorm(6), qr = rchisq(6, 10))),
          list(data.frame(height = rnorm(8), weight = runif(8)), data.frame(nr = rnorm(8), qr = rchisq(8, 10))))


[[1]]
[[1]][[1]]
        height      weight
1  -0.49424331 0.023996582
2  -0.80320654 0.029460558
3  -0.89797434 0.932508002
4  -0.25267069 0.790625104
5   0.27474082 0.859495769
6   0.14285128 0.009731295
7  -0.86224008 0.343969165
8   0.07358127 0.106006154
9  -1.61474408 0.302890840
10  2.23920173 0.133115944

[[1]][[2]]
            nr        qr
1  -1.24342871 10.278033
2   1.37520549 13.246929
3  -0.06046197  6.267480
4   0.73643661 14.084240
5  -0.01897590  1.323470
6   1.10877385 11.739945
7  -1.09511298 10.616714
8  -1.03525533  9.992008
9  -0.04301281 12.943073
10 -0.79446848  8.670066


[[2]]
[[2]][[1]]
      height    weight
1 -2.7323741 0.8825884
2  0.4745896 0.1813869
3  0.9158570 0.9660507
4  0.8927806 0.1156805
5 -0.8443665 0.3079322
6 -0.4703602 0.2345349

[[2]][[2]]
          nr        qr
1 -1.6915651  9.532319
2 -1.9810859 15.145930
3 -0.4890531 10.013549
4  0.2163449 13.407265
5  1.0770555  6.676846
6 -0.5431102  7.688177


[[3]]
[[3]][[1]]
      height     weight
1 -1.2671410 0.48468152
2 -0.7792946 0.04499799
3 -0.6976782 0.10917336
4  0.8274744 0.69698260
5 -0.9456592 0.64183451
6 -1.2882436 0.29868696
7  0.6424889 0.86165232
8 -0.8255187 0.16430852

[[3]][[2]]
          nr        qr
1 -0.4160331  5.341376
2 -1.0321303  8.947948
3 -0.3380597  7.937599
4 -2.1520878 11.740298
5 -0.8979710  2.393419
6 -1.1172138 11.780884
7 -1.0309391  2.673642
8  0.8822399 12.351724

I want to transform it so that all the data.frames with (height, weight) columns are rbinded together and all the (nr, qr) data.frames are rbinded together. So basically first element of each list in the list should be binded together and the second element of each list in the list should be binded together.

Expected Output would be another list which will have two data.frames like following:-

[[1]]
        height      weight
1  -0.49424331 0.023996582
2  -0.80320654 0.029460558
3  -0.89797434 0.932508002
4  -0.25267069 0.790625104
5   0.27474082 0.859495769
6   0.14285128 0.009731295
7  -0.86224008 0.343969165
8   0.07358127 0.106006154
9  -1.61474408 0.302890840
10  2.23920173 0.133115944
11 -2.7323741 0.8825884
12  0.4745896 0.1813869
13  0.9158570 0.9660507
14  0.8927806 0.1156805
15 -0.8443665 0.3079322
16 -0.4703602 0.2345349
17 -1.2671410 0.48468152
18 -0.7792946 0.04499799
19 -0.6976782 0.10917336
20  0.8274744 0.69698260
21 -0.9456592 0.64183451
22 -1.2882436 0.29868696
23  0.6424889 0.86165232
24 -0.8255187 0.16430852

[[2]]
            nr        qr
1  -1.24342871 10.278033
2   1.37520549 13.246929
3  -0.06046197  6.267480
4   0.73643661 14.084240
5  -0.01897590  1.323470
6   1.10877385 11.739945
7  -1.09511298 10.616714
8  -1.03525533  9.992008
9  -0.04301281 12.943073
10 -0.79446848  8.670066
11 -1.6915651  9.532319
12 -1.9810859 15.145930
13 -0.4890531 10.013549
14  0.2163449 13.407265
15  1.0770555  6.676846
16 -0.5431102  7.688177
17 -0.4160331  5.341376
18 -1.0321303  8.947948
19 -0.3380597  7.937599
20 -2.1520878 11.740298
21 -0.8979710  2.393419
22 -1.1172138 11.780884
23 -1.0309391  2.673642
24  0.8822399 12.351724

CodePudding user response:

This should do it.

dd <- list(do.call(rbind, lapply(d, "[[", 1)), do.call(rbind, lapply(d, "[[", 2)))
  • Related