假设我有一个数据集作为列表对象。这是一种快速生成一些随机数据的方法:
a <- list(x1=rnorm(10),x2=rnorm(10))
b <- list(y1=rnorm(10),y2=rnorm(10),y3=rnorm(10))
c <- list(x1=rnorm(10),x2=rnorm(10))
d <- list(y1=rnorm(10),y2=rnorm(10),y3=rnorm(10))
e <- list(x1=rnorm(10),x2=rnorm(10))
f <- list(y1=rnorm(10),y2=rnorm(10),y3=rnorm(10))
branch1 <- list(a,b)
branch2 <- list(c,d)
branch3 <- list(e,f)
data <- list(branch1,branch2,branch3)
数据将如下所示:
> data
[[1]]
[[1]][[1]]
[[1]][[1]]$x1
[1] -0.04944765 -2.52337788 0.30997940 -0.71163967 0.48528227 1.17816655 0.84655561 -2.17030509
[9] -0.03489831 -0.18292103
[[1]][[1]]$x2
[1] -0.96112288 -0.57049890 -1.08079903 -3.12189611 0.04508508 -0.38865962 1.61032327 -0.63870639
[9] 0.77297993 0.48404346
[[1]][[2]]
[[1]][[2]]$y1
[1] 1.1722426 -0.9313285 2.7288262 1.9429077 0.4842252 1.4551183 0.2364701 0.8246233 -0.8540208
[10] 1.9039492
[[1]][[2]]$y2
[1] 1.07758478 -0.90898684 1.00826795 0.97841961 0.70665070 0.05041242 -0.88985476 1.46972378
[9] -2.38069537 1.87440491
[[1]][[2]]$y3
[1] -0.08308981 -0.37208124 0.70363404 -0.88521094 -0.54751152 0.43910681 0.35753713 -0.83412426
[9] -1.12528856 -1.61850682
[[2]]
[[2]][[1]]
[[2]][[1]]$x1
[1] 0.01377682 0.19047533 -0.17129543 0.56909904 -0.50928670 1.11754925 0.43099918 -0.37832047
[9] -0.56111033 0.73358678
[[2]][[1]]$x2
[1] 2.3302948 -0.9129171 0.7549973 -1.0379182 -1.9319941 -0.7924752 0.6500014 0.4178630 1.3118695
[10] -1.2121514
[[2]][[2]]
[[2]][[2]]$y1
[1] 0.8275016 0.7133281 -0.5240551 1.6891841 -0.5621567 -0.7496414 0.5983334 1.3617800 -0.4837031
[10] 0.4461964
[[2]][[2]]$y2
[1] -0.8155957 2.0451346 0.5882782 -0.4417398 -0.2335832 -1.4218698 -1.3949293 0.6129234 -0.6052243
[10] 0.1414337
[[2]][[2]]$y3
[1] -0.3374557 0.3310320 -0.8983646 1.7219435 0.2828211 0.1116751 0.6373569 0.3369811 -1.7301219
[10] 0.6329963
[[3]]
[[3]][[1]]
[[3]][[1]]$x1
[1] 0.8392581 -1.0852638 0.2278769 -0.2653210 0.1231531 0.7964941 -1.0282257 1.0272565 0.6882902
[10] 0.2056550
[[3]][[1]]$x2
[1] 1.03245491 -0.06994712 -0.53785824 -1.00255262 -0.22105177 -1.07233849 0.93624906 1.03518821
[9] 1.24065942 -3.51732843
[[3]][[2]]
[[3]][[2]]$y1
[1] -0.30520118 0.60494774 -1.45178410 -0.32739980 -0.15773973 0.76237976 -0.04449191 0.31864511
[9] -0.12763899 -0.78701147
[[3]][[2]]$y2
[1] 0.02523319 1.10718390 -0.24078229 1.44386520 0.52889358 0.04096228 -0.05084876 0.81393090
[9] 2.14785155 -2.23776686
[[3]][[2]]$y3
[1] 2.62345162 -0.07802202 0.12745068 -0.92882646 -0.09972968 0.24213258 0.48338620 0.38180029
[9] 1.09133254 -0.29834029
假设我想从每个列表中提取 y1、y2 和 y3 并形成如下所示的数据框:
ID value1 value2 value3
1 y1 y2 y3
2 y1 y2 y3
3 y1 y2 y3
4 y1 y2 y3
5 y1 y2 y3
6 y1 y2 y3
... ... ... ...
我怎么能这样做?