- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the mean of list elements in R?
To find the mean of list elements we need to unlist those elements. For example, if we have a list named as List that contains three elements of equal or different sizes such element1, element2, and element3 then we can find the mean of all the list elements by using mean(unlist(List)).
Example1
List1<−list(x1=rnorm(20,5,1),x2=rexp(20,1.5),x3=runif(20,1,5),x4=rpois(20,5)) List1
Output
$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
Example2
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
Output
$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
Example3
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
Output
$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
Example4
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
Output
$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
- Related Articles
- How to find the mean of list elements without unlisting them in R?
- How to find the mean of corresponding elements of multiple matrices in R?
- How to find the mean of all matrices stored in an R list?
- How to find the sum of two list elements in R?
- How to find the mean of a square matrix elements by excluding diagonal elements in R?
- How to extract the names of list elements in R?
- How to find the standard error of mean in R?
- How to find the groupwise cumulative mean in R?
- How to find the mean of three-dimensional array in R?
- How to change the order of elements in a list in R?
- How to select multiple elements of a list in R?
- How to combine list elements of different sizes in R?
- How to find the root mean square of a vector in R?
- How to find the mean of all columns by group in R?
- How to find the mean of all values in an R data frame?

Advertisements