为了找到列表元素的平均值,我们需要取消列出那些元素。例如,如果我们有一个名为List的列表,其中包含三个大小相等或不同的元素,例如element1,element2和element3,则可以使用mean(unlist(List))找到所有列表元素的均值。
List1<−list(x1=rnorm(20,5,1),x2=rexp(20,1.5),x3=runif(20,1,5),x4=rpois(20,5)) List1
输出结果
$x1 [1] 6.108059 4.547547 4.372156 5.989584 5.511777 4.709440 7.021406 6.039822 [9] 4.543087 4.336187 5.042706 4.706642 4.956130 3.778773 4.132992 3.940984 [17] 6.065076 4.612344 4.920258 4.329872 $x2 [1] 0.53495402 0.02912564 0.36639652 0.24274400 0.39298863 0.19216387 [7] 0.28707176 0.60966760 0.12513682 0.32347674 0.84902529 0.81149732 [13] 0.37236099 0.48662762 0.23458569 1.64478209 2.10184808 1.27785900 [19] 0.93100703 0.87303565 $x3 [1] 1.912571 4.163985 3.969263 2.059990 1.199151 3.909256 1.554315 2.895359 [9] 2.086084 2.445037 2.576525 3.065689 3.300594 4.653713 3.849018 2.128604 [17] 3.972365 2.346908 1.345889 1.548029 $x4 [1] 7 4 4 2 5 5 5 6 4 6 7 4 3 7 9 4 2 6 4 6 mean(unlist(List1)) [1] 3.341669
List2<−list(y1=rpois(25,5),y2=rpois(25,10),y3=rpois(25,3),y4=rpois(25,2),y5=rpois(25,12),y6=rpois(25,8)) List2
输出结果
$y1 [1] 5 8 1 7 9 3 4 4 6 3 3 5 2 6 6 5 7 4 3 7 4 2 4 4 1 $y2 [1] 6 9 16 12 9 14 8 5 9 11 14 10 18 9 10 6 5 9 11 9 8 11 8 12 8 $y3 [1] 1 3 3 3 9 6 0 3 2 6 2 2 4 1 1 2 4 1 3 2 1 5 3 5 2 $y4 [1] 1 3 0 1 0 0 0 2 2 2 1 2 1 1 5 0 1 2 3 1 3 3 1 1 3 $y5 [1] 7 17 11 15 9 12 9 15 11 9 12 13 8 14 13 15 10 12 12 11 11 7 11 9 20 $y6 [1] 7 8 5 8 6 12 10 7 11 15 7 7 7 17 7 11 4 5 15 7 4 5 9 13 8 mean(unlist(List2)) [1] 6.54
List3<−list(z1=sample(0:2,50,replace=TRUE),z2=sample(1:5,50,replace=TRUE),z3=sample(1:10,50,replace=TRUE),z4=sample(11:20,50,replace=TRUE),z5=sample(1:100,20)) List3
输出结果
$z1 [1] 1 1 2 0 1 0 2 1 0 1 2 1 1 1 2 0 0 0 0 2 2 2 0 2 0 1 1 2 1 2 0 0 1 0 0 2 0 2 [39] 2 2 1 1 0 0 1 2 0 1 2 1 $z2 [1] 2 2 4 3 4 4 4 2 2 5 5 3 1 2 1 3 1 1 5 4 5 2 2 2 5 4 5 4 1 1 3 2 2 2 5 3 4 4 [39] 3 1 2 5 2 1 5 1 3 5 3 1 $z3 [1] 2 1 6 6 9 3 10 5 3 2 7 3 9 1 7 7 9 3 10 6 9 6 7 1 2 [26] 2 7 8 9 9 10 10 4 6 9 1 4 4 1 3 6 7 5 1 6 4 7 9 7 8 $z4 [1] 14 20 12 19 20 20 19 14 19 16 14 15 18 19 18 16 17 16 15 19 15 15 18 16 16 [26] 16 13 13 19 19 18 12 19 14 11 11 16 16 17 17 11 11 12 16 18 14 12 12 14 16 $z5 [1] 84 3 91 98 8 26 5 62 77 79 44 80 95 22 33 94 48 100 18 [20] 82 mean(unlist(List3)) [1] 10.96364
List4<−list(a1=rlnorm(30,20,2),a2=rlnorm(30,40,3.5),a3=rlnorm(30,32,4.87),a4=rlnorm(30,25,2)) List4
输出结果
$a1 [1] 71542867 3802528183 824955139 537941797 319344620 9780278 [7] 412319119 3651501904 157606770 23725836 160733494 120731549 [13] 7069722553 25051170 541634971 75770086507 1832990477 1475938038 [19] 49199098 5368002989 11307747 52684854 1292534007 433447010 [25] 875482499 113172333 230498945 4929889507 4141435472 703825036 $a2 [1] 1.877107e+14 2.945731e+18 7.871179e+17 2.734189e+18 6.543502e+15 [6] 7.634623e+19 1.119141e+18 6.297200e+15 8.668692e+18 1.974060e+19 [11] 5.513059e+18 7.012868e+14 6.418158e+15 3.669615e+17 7.844477e+17 [16] 1.071829e+14 1.846123e+19 3.174034e+15 2.965029e+19 1.611821e+18 [21] 3.977186e+17 5.586130e+18 3.497191e+17 3.341409e+18 4.465099e+20 [26] 2.118766e+18 2.937740e+17 1.212326e+16 3.861193e+17 1.842242e+16 $a3 [1] 2.360510e+13 4.108881e+12 2.561652e+15 3.753383e+14 8.076318e+13 [6] 1.119506e+12 2.409259e+12 8.971752e+12 2.224645e+14 9.817681e+15 [11] 2.081065e+12 2.216867e+14 1.323823e+14 1.271452e+14 1.744322e+15 [16] 1.157680e+18 4.779613e+11 4.156495e+17 2.361336e+14 1.040876e+14 [21] 2.130798e+15 3.491057e+13 3.638596e+12 7.101385e+12 9.217809e+11 [26] 5.588433e+12 1.319278e+15 9.346423e+11 2.446196e+12 1.985828e+16 $a4 [1] 3.338469e+10 5.547337e+11 5.587813e+10 7.720164e+10 6.328412e+11 [6] 6.939032e+09 8.160043e+09 1.367509e+12 2.770085e+11 4.999626e+11 [11] 2.993918e+10 4.003077e+10 1.966285e+12 4.918578e+09 1.110343e+11 [16] 1.221764e+10 2.352072e+09 3.829187e+09 4.752032e+11 3.299800e+11 [21] 2.154089e+10 1.607422e+10 2.489736e+09 9.573338e+09 1.438301e+10 [26] 1.564655e+10 7.064011e+10 1.458467e+11 1.362668e+08 1.376267e+12 mean(unlist(List4)) [1] 5.244828e+18