列表中具有相同索引的 `rbind` 元素
`rbind` elements with same index in a list
我有一个 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.frame
都 rbind
ed 在一起,并且所有 (nr, qr) data.frame
s 都是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
这应该可以做到。
dd <- list(do.call(rbind, lapply(d, "[[", 1)), do.call(rbind, lapply(d, "[[", 2)))
我有一个 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.frame
都 rbind
ed 在一起,并且所有 (nr, qr) data.frame
s 都是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
这应该可以做到。
dd <- list(do.call(rbind, lapply(d, "[[", 1)), do.call(rbind, lapply(d, "[[", 2)))