列表中具有相同索引的“rbind”元素

发布于 2025-01-10 02:44:06 字数 3431 浏览 0 评论 0原文

我有一个 list,其中包含三个 list,每个列表都有 2 个 data.frames,如下所示:-

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

我想对其进行转换,以便所有具有 (height, Weight) 列的 data.framerbind 在一起,并且所有 (nr, qr) data.frame 都是rbind 在一起。因此基本上列表中每个列表的第一个元素应该绑定在一起,并且列表中每个列表的第二个元素应该绑定在一起。

预期输出将是另一个列表,其中将有两个 data.frame ,如下所示:-

[[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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵强ㄟ 2025-01-17 02:44:06

这应该可以做到。

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

This should do it.

dd <- list(do.call(rbind, lapply(d, "[[", 1)), do.call(rbind, lapply(d, "[[", 2)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文