- 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 without unlisting them in R?
Most of the times, unlisting is used to find the mean of list elements but we can also use double-square brackets for the same purpose. The double-square brackets are basically used to access the values in the elements of the list, thus mean function works with those values directly. Look at the below example to understand how it works.
Example
Consider the below list:
> x<-rpois(50,5) > x
Output
[1] 3 3 3 5 3 1 4 7 5 4 5 9 9 7 4 3 6 2 4 3 3 4 7 4 4 [26] 4 5 3 4 4 3 5 7 2 3 4 7 9 6 3 2 7 12 8 3 2 4 3 7 7
Example
> y<-rpois(75,8) > y
Output
[1] 10 13 3 4 8 13 9 7 6 6 10 8 6 6 2 8 11 18 7 6 9 9 4 11 6 [26] 15 7 6 8 6 7 3 6 7 8 10 5 5 5 6 9 11 11 9 4 9 7 4 11 7 [51] 7 8 8 6 5 4 10 10 10 11 8 5 9 13 12 6 15 12 9 10 9 10 8 9 11
Example
> z<-sample(0:10,70,replace=TRUE) > z
Output
[1] 1 2 5 1 5 10 3 3 0 3 4 6 1 10 10 10 7 9 4 4 6 8 8 2 10 [26] 3 10 7 5 7 7 10 8 4 5 0 1 7 4 3 6 7 3 6 9 4 6 2 1 2 [51] 8 3 4 3 7 1 5 0 2 1 6 1 8 7 2 1 0 9 1 5
Example
> a<-rnorm(70,5,1) > a
Output
[1] 5.628975 5.573691 5.006051 5.077979 5.871554 5.385300 4.657602 4.735646 [9] 4.442138 4.760412 5.017734 4.280068 7.686220 3.875272 4.067216 5.701463 [17] 5.593412 5.775659 5.879765 5.016738 3.994980 5.155600 6.712727 4.389124 [25] 6.122981 4.864763 3.368189 5.707907 3.795154 5.352383 5.916437 4.626415 [33] 3.880298 4.557015 4.811892 6.055774 7.219117 4.646261 4.674638 6.131624 [41] 4.965561 4.821740 3.863569 3.450784 5.343807 3.379690 4.184890 5.120902 [49] 5.729253 4.493643 4.401883 3.231263 4.858176 2.979016 3.687358 4.505526 [57] 4.562880 6.275943 6.123173 7.069203 3.584879 4.142615 5.263056 4.817530 [65] 5.623996 4.190001 3.751704 2.850223 3.585672 4.894851
Example
> b<-runif(80,3,8) > b
Output
[1] 3.442903 4.001856 6.432766 4.891293 5.044795 5.218431 3.171832 4.397526 [9] 6.285000 6.379085 5.709033 6.291581 4.218582 4.236623 4.174158 5.074241 [17] 6.673121 7.885147 5.019942 6.679726 3.431944 3.822653 5.507297 5.496158 [25] 5.514768 4.815769 3.499057 4.669202 4.775597 7.990668 6.883988 6.130633 [33] 6.777758 3.015941 6.189278 4.884390 6.961173 4.642663 6.739331 4.135479 [41] 7.120461 6.584457 4.597551 4.988885 3.520279 6.762352 5.131450 7.817774 [49] 7.881543 4.903794 5.692226 4.195919 4.629501 3.668161 3.138173 4.645494 [57] 7.514812 5.957177 6.617235 6.490214 5.175956 4.944264 6.886052 7.902101 [65] 6.308360 3.615881 5.029082 6.779920 7.993889 4.205757 3.185573 3.111127 [73] 7.556962 3.458693 7.766828 7.935259 5.964028 4.831651 6.164589 6.658785
Example
> c<-rexp(60,3.12) > c
Output
[1] 0.1047474880 0.1613035476 0.2720461048 0.3714672737 0.1450241494 [6] 0.1172384887 0.0929828154 0.0664085124 0.1664490039 0.1463586694 [11] 0.5154090083 0.3250343993 0.0129845417 0.1228420892 0.0005871378 [16] 0.4522178841 0.0405094619 0.0640481666 0.1504493882 0.0456359051 [21] 0.3061571805 0.2142645778 0.0006100014 0.8694030410 0.1857693661 [26] 0.8603525043 0.5723388268 0.1471301074 0.3497110378 0.0989022598 [31] 0.7969995083 0.1855059460 0.3252937372 0.0620924534 0.5784960475 [36] 0.5985177050 0.0473140977 0.4036286207 0.0482094750 0.0045920645 [41] 0.1998384091 0.3897105935 0.1562889333 0.3647960092 0.2741774798 [46] 0.0256578746 0.3201171403 0.2401259122 0.7529862278 0.3503083498 [51] 0.1387969009 0.0194571765 0.0571447048 0.4248323683 0.3069464950 [56] 0.2832176146 0.0090127322 0.0379236670 0.3523537577 0.2085964980
Example
> w<-sample(1:100,65) > w
Output
[1] 58 94 72 56 33 29 9 28 60 42 97 40 67 98 78 68 43 2 10 [20] 53 79 27 47 1 41 84 34 99 92 19 93 37 13 64 51 3 46 52 [39] 59 35 73 30 80 17 95 76 15 44 81 21 55 74 83 6 77 36 8 [58] 65 63 45 14 71 100 61 90
Example
> q<-round(rnorm(150),0) > q
Output
[1] -1 -1 0 0 0 1 0 -2 -1 0 1 0 0 -1 1 1 1 2 2 -1 -1 0 1 1 1 [26] -3 -1 0 0 2 0 1 0 -1 1 0 0 1 2 2 1 0 1 0 1 1 0 1 0 1 [51] 0 1 0 0 1 1 2 0 1 1 -1 0 1 1 0 1 0 0 0 0 0 -1 0 1 0 [76] 1 1 1 -2 -1 1 0 0 0 2 -1 1 0 0 0 -1 0 0 -2 2 0 1 1 -1 1 [101] -3 -1 -1 1 1 -2 0 0 1 0 0 1 -1 0 2 -1 -1 1 0 1 -1 0 0 -1 0 [126] 0 1 -1 -2 1 0 1 -1 0 -1 0 -2 -1 0 1 -1 1 1 -1 0 -2 -1 2 -2 0
Example
> List<-list(x,y,z,a,b,c,w,q) > List
Output
[[1]] [1] 3 3 3 5 3 1 4 7 5 4 5 9 9 7 4 3 6 2 4 3 3 4 7 4 4 [26] 4 5 3 4 4 3 5 7 2 3 4 7 9 6 3 2 7 12 8 3 2 4 3 7 7 [[2]] [1] 10 13 3 4 8 13 9 7 6 6 10 8 6 6 2 8 11 18 7 6 9 9 4 11 6 [26] 15 7 6 8 6 7 3 6 7 8 10 5 5 5 6 9 11 11 9 4 9 7 4 11 7 [51] 7 8 8 6 5 4 10 10 10 11 8 5 9 13 12 6 15 12 9 10 9 10 8 9 11 [[3]] [1] 1 2 5 1 5 10 3 3 0 3 4 6 1 10 10 10 7 9 4 4 6 8 8 2 10 [26] 3 10 7 5 7 7 10 8 4 5 0 1 7 4 3 6 7 3 6 9 4 6 2 1 2 [51] 8 3 4 3 7 1 5 0 2 1 6 1 8 7 2 1 0 9 1 5 [[4]] [1] 5.628975 5.573691 5.006051 5.077979 5.871554 5.385300 4.657602 4.735646 [9] 4.442138 4.760412 5.017734 4.280068 7.686220 3.875272 4.067216 5.701463 [17] 5.593412 5.775659 5.879765 5.016738 3.994980 5.155600 6.712727 4.389124 [25] 6.122981 4.864763 3.368189 5.707907 3.795154 5.352383 5.916437 4.626415 [33] 3.880298 4.557015 4.811892 6.055774 7.219117 4.646261 4.674638 6.131624 [41] 4.965561 4.821740 3.863569 3.450784 5.343807 3.379690 4.184890 5.120902 [49] 5.729253 4.493643 4.401883 3.231263 4.858176 2.979016 3.687358 4.505526 [57] 4.562880 6.275943 6.123173 7.069203 3.584879 4.142615 5.263056 4.817530 [65] 5.623996 4.190001 3.751704 2.850223 3.585672 4.894851 [[5]] [1] 3.442903 4.001856 6.432766 4.891293 5.044795 5.218431 3.171832 4.397526 [9] 6.285000 6.379085 5.709033 6.291581 4.218582 4.236623 4.174158 5.074241 [17] 6.673121 7.885147 5.019942 6.679726 3.431944 3.822653 5.507297 5.496158 [25] 5.514768 4.815769 3.499057 4.669202 4.775597 7.990668 6.883988 6.130633 [33] 6.777758 3.015941 6.189278 4.884390 6.961173 4.642663 6.739331 4.135479 [41] 7.120461 6.584457 4.597551 4.988885 3.520279 6.762352 5.131450 7.817774 [49] 7.881543 4.903794 5.692226 4.195919 4.629501 3.668161 3.138173 4.645494 [57] 7.514812 5.957177 6.617235 6.490214 5.175956 4.944264 6.886052 7.902101 [65] 6.308360 3.615881 5.029082 6.779920 7.993889 4.205757 3.185573 3.111127 [73] 7.556962 3.458693 7.766828 7.935259 5.964028 4.831651 6.164589 6.658785 [[6]] [1] 0.1047474880 0.1613035476 0.2720461048 0.3714672737 0.1450241494 [6] 0.1172384887 0.0929828154 0.0664085124 0.1664490039 0.1463586694 [11] 0.5154090083 0.3250343993 0.0129845417 0.1228420892 0.0005871378 [16] 0.4522178841 0.0405094619 0.0640481666 0.1504493882 0.0456359051 [21] 0.3061571805 0.2142645778 0.0006100014 0.8694030410 0.1857693661 [26] 0.8603525043 0.5723388268 0.1471301074 0.3497110378 0.0989022598 [31] 0.7969995083 0.1855059460 0.3252937372 0.0620924534 0.5784960475 [36] 0.5985177050 0.0473140977 0.4036286207 0.0482094750 0.0045920645 [41] 0.1998384091 0.3897105935 0.1562889333 0.3647960092 0.2741774798 [46] 0.0256578746 0.3201171403 0.2401259122 0.7529862278 0.3503083498 [51] 0.1387969009 0.0194571765 0.0571447048 0.4248323683 0.3069464950 [56] 0.2832176146 0.0090127322 0.0379236670 0.3523537577 0.2085964980 [[7]] [1] 58 94 72 56 33 29 9 28 60 42 97 40 67 98 78 68 43 2 10 [20] 53 79 27 47 1 41 84 34 99 92 19 93 37 13 64 51 3 46 52 [39] 59 35 73 30 80 17 95 76 15 44 81 21 55 74 83 6 77 36 8 [58] 65 63 45 14 71 100 61 90 [[8]] [1] -1 -1 0 0 0 1 0 -2 -1 0 1 0 0 -1 1 1 1 2 2 -1 -1 0 1 1 1 [26] -3 -1 0 0 2 0 1 0 -1 1 0 0 1 2 2 1 0 1 0 1 1 0 1 0 1 [51] 0 1 0 0 1 1 2 0 1 1 -1 0 1 1 0 1 0 0 0 0 0 -1 0 1 0 [76] 1 1 1 -2 -1 1 0 0 0 2 -1 1 0 0 0 -1 0 0 -2 2 0 1 1 -1 1 [101] -3 -1 -1 1 1 -2 0 0 1 0 0 1 -1 0 2 -1 -1 1 0 1 -1 0 0 -1 0 [126] 0 1 -1 -2 1 0 1 -1 0 -1 0 -2 -1 0 1 -1 1 1 -1 0 -2 -1 2 -2 0
Finding the mean of each element in the list:
Example
> mean(List[[1]])
Output
[1] 4.72
Example
> mean(List[[2]])
Output
[1] 8.146667
Example
> mean(List[[3]])
Output
[1] 4.757143
Example
> mean(List[[4]])
Output
[1] 4.882414
Example
> mean(List[[5]])
Output
[1] 5.480545
Example
> mean(List[[6]])
Output
[1] 0.249022
Example
> mean(List[[7]])
Output
[1] 52.2
Example
> mean(List[[8]])
Output
[1] 0.1066667
Advertisements