R - 对列表中相应矢量项的操作

时间:2018-02-17 20:06:20

标签: r list vector apply

我们说我有一个向量列表,如下所示:

[[1]]
 [1] -0.36603596 -0.41461025 -0.68573296 -0.55516173  0.05071238  0.47723472  0.10851948
 [8]  0.67005116  0.25519780 -0.79428716  0.16506077  0.81905548  0.22808934 -0.39257712
[15]  0.44778539 -0.36149934 -0.90142102 -0.99826169  0.24544167 -0.18989310 -0.67592344
[22] -0.65447808  0.26617179 -0.25020153  0.19562031  0.53520465 -0.47531100 -0.60152887
[29]  0.12012461 -0.68947499 -0.33258301  0.19914520 -0.70396942  0.21574644 -0.67197365
[36] -0.12744723 -0.07113916  0.44497439  0.07592963 -0.29082130 -0.27967624  0.28314801
[43] -0.09840383 -0.55582233 -0.29474315 -0.41717316  0.51017306 -0.31227399  0.39484400
[50] -0.88843530

[[2]]
 [1] -0.14763873 -0.69009083 -0.55705599 -0.43779047  0.15626341 -0.00629513 -0.95227841
 [8]  0.85645849 -0.40110676 -0.35732008  0.31375323  0.71478975  0.02262899 -0.12802829
[15]  0.58750725 -0.25629463 -0.65609956 -0.83185625 -0.35244759 -0.33287717 -0.99199682
[22] -0.45836093 -0.19431609 -0.41590652  1.06120542  0.20687783  0.13268137 -0.34219985
[29] -0.18096691 -0.24496102 -0.47769117  0.89134577 -0.56128402  0.70825268  0.10426368
[36] -0.13962506 -0.72478276 -0.40178315  0.65943132 -0.82083464  0.22569929 -1.02243310
[43] -0.70983610 -1.36733592  0.68807554  0.09156598  0.76850778 -0.64040433  0.79276407
[50] -0.40297792

[[3]]
 [1]  0.34405450 -0.07928067  0.08353835 -0.37919066 -0.47233278 -0.38839824 -0.13269067
 [8]  0.17348495  0.42777652 -0.19297300 -0.86438130  0.75787336 -0.34358747  0.47852682
[15]  1.29980892 -0.42527812 -0.25074922 -0.59565850  0.32800193 -0.56109570 -0.72905476
[22] -0.11498356 -0.29827083 -0.21653428  0.78533418  0.64735755  0.31889828 -0.37129803
[29] -0.51252162  0.24192268 -0.29281809  1.03299397 -0.11251429  0.13157698 -0.06404053
[36]  0.01904473 -0.13162565  0.30488937  0.31933970  0.14135025 -0.31501649  0.16738399
[43] -0.19627252 -1.29613018 -0.03572980 -0.72008672  0.13932428 -0.06117093 -0.62665670
[50] -0.12662761

[[4]]
 [1]  0.183303468  0.160037845 -0.053473912  0.005199917 -0.126312554  0.116465956 -0.061730281
 [8]  0.392903969 -0.008337453 -0.752631038 -0.235599857  0.999534398  0.375208363  0.201100799
[15]  0.444068886 -0.575795949 -0.873388633 -0.863612264  0.076050073 -0.188358603 -0.391865671
[22] -1.726690292 -1.206992567 -0.547175750  0.290255919  1.119834989  0.551360182 -0.510140345
[29] -0.460314706 -0.245835558 -0.315087602  0.947181076 -0.132550448  0.038419545 -0.017929636
[36]  0.041870497 -0.520961791  0.195326850 -0.117783785 -0.427426472 -0.119577158  0.702550914
[43] -0.045789957 -0.794299036  0.181420440  0.407347072  0.571894407 -0.217325835  0.280283391
[50] -0.492866084

[[5]]
 [1] -0.40852268 -0.33488615 -0.30609700 -0.67467326 -0.11966383  1.01161858 -0.27108333
 [8]  0.92772286  0.39047166  0.29019594  0.24404167  0.07824440  0.32786441  0.21657727
[15]  0.34362648 -0.44996166 -0.27823770 -1.24962127 -0.57241699 -0.30297804 -0.66728157
[22]  0.01783441  0.50773758 -0.31477033 -0.14581338 -0.13827194 -0.25574117  0.40049840
[29]  0.38634920 -0.29027963 -0.03381480  0.48510557 -0.61594522  1.09573928 -0.27992008
[36] -0.41523542 -0.24131548  0.43480320  0.32855110  0.48579320  0.47366867  0.62697303
[43] -0.57792202 -0.81951194  0.21583044  0.15593484 -0.10270703 -0.10206812 -0.25195873
[50] -0.89835763

我想平均相应的矢量项(例如:[[1]] [1],[[1]] [2],[[1]] [3]等)得到一个矢量平均值。例如,列表中每个第一个矢量项的平均值为-0.07896788。最好的方法是什么?

1 个答案:

答案 0 :(得分:1)

假设列表名为mylist:

mydf=as.data.frame(do.call("rbind",mylist))
colMeans(mydf)

那会是理想的输出吗?