是否有机会从包含更多列表的列表中获取数据帧? 我的数据如下:
> head(list,3)
[[1]]
7 36 37 41 42 47 48 49 58
0.0162201303 0.0072411296 0.0144822592 0.0175235337 0.0001448226 0.3943519189 0.0043446778 0.0013034033 0.0097031137
61 87 132 134 143 144 145 146 147
0.0041998552 0.0005792904 0.0008689356 0.0002896452 0.1963794352 0.0002896452 0.0085445329 0.0108616944 0.0062273715
148 149 150 151 152 153 163 170 171
0.0224475018 0.0043446778 0.0273714699 0.0004344678 0.0004344678 0.0069514844 0.0002896452 0.0083997104 0.0031860970
172 173 174 175 176 177 179
0.0037653874 0.1863866763 0.0169442433 0.0047791455 0.0186821144 0.0002896452 0.0017378711
[[2]]
7 36 37 47 48 49 58 61 87
3.493614e-02 6.260957e-05 6.260957e-05 2.948911e-02 8.139244e-04 1.001753e-03 1.252191e-04 1.001753e-03 6.260957e-05
88 89 91 92 93 94 95 96 97
1.252191e-04 6.260957e-05 7.951415e-03 5.634861e-03 1.296018e-02 2.504383e-04 5.071375e-03 5.256699e-01 4.695718e-03
99 109 110 134 143 144 145 146 147
7.012271e-03 6.448785e-03 1.252191e-04 3.130478e-04 3.156148e-01 6.260957e-05 2.880040e-03 3.130478e-03 9.015778e-03
148 149 150 151 152 153
5.384423e-03 1.001753e-02 3.881793e-03 1.878287e-04 6.260957e-05 5.885299e-03
[[3]]
7 36 37 38 41 42 47 49 58 61
0.009215442 0.002241594 0.004234122 0.000747198 0.007721046 0.000249066 0.129514321 0.002490660 0.002988792 0.000747198
62 91 92 93 94 95 96 97 99 109
0.000996264 0.018929016 0.001494396 0.041344956 0.000498132 0.008966376 0.709589041 0.002241594 0.053300125 0.001494396
110 132 134
0.000249066 0.000249066 0.000498132
我希望有一个如下所示的数据框:
ID Var1 Var 2 Var 3
7 0.016 0.0035 0.009
36 0.007 0.0000 0.002
37 0.014 0.0000 0.004
38 NA NA 0.000
41 0.0175 NA 0.007
42 0.00014 NA 0.000
47 0.39 NA 0.13
...
这可能吗?
答案 0 :(得分:3)
您可以使用melt/dcast
reshape2
library(reshape2)
dcast(melt(lapply(l1, function(x) as.data.frame.list(x))),
variable~paste0('Var',L1), value.var='value')
# variable Var1 Var2 Var3
#1 X7 3 NA 8
#2 X9 2 NA 10
#3 X1 6 9 NA
#4 X5 9 2 9
#5 X2 3 NA NA
#6 X3 5 8 5
#7 X6 2 7 10
#8 X10 NA 10 5
#9 X4 NA 2 4
#10 X8 NA NA 10
@Ananda Mahto建议的变体
dcast(melt(lapply(l1, as.matrix)), Var1 ~ L1, value.var = "value")
或者您可以使用rbindlist
来提供wider
数据集
library(data.table)
rbindlist(lapply(l1, function(x) as.data.frame.list(x)), fill=TRUE)
# X7 X9 X1 X5 X2 X3 X6 X10 X4 X8
#1: 3 2 6 9 3 5 2 NA NA NA
#2: NA NA 9 2 NA 8 7 10 2 NA
#3: 8 10 NA 9 NA 5 10 5 4 10
set.seed(49)
l1 <- lapply(1:3 ,function(i) {x1 <- sample(1:10,sample(5:10),replace=TRUE)
names(x1) <- sample(1:10, length(x1),replace=FALSE)
x1})