How to find the mean of every n values in an R vector?

R ProgrammingServer Side ProgrammingProgramming

To find the mean of every n values in an R vector, we would first need to convert the vector into matrix with number of rows based for n values then we can use colMeans function.

For example, if we have a vector called X that contains hundred values then we can find the mean of every ten value by using the command given below −

colMeans(matrix(X,10))

Example 1

To find the mean of every n values in an R vector, use the command given below −

x1<-rnorm(500)
colMeans(matrix(x1,10))

Output

If you execute the above given snippet, it generates the following output −

[1] 0.06042820 0.10300658 0.26501027 0.11565051 -0.23118591 0.16860741
[7] 0.41769403 0.04953221 0.32198954 -0.30868446 0.32225750 -0.42216023
[13] 0.26006613 0.24474911 -0.43471863 -0.20061558 -0.04179246 0.09600802
[19] -0.23894397 -0.49444600 0.05066381 -0.46426885 -0.20500667 -0.56426654
[25] 0.46374940 0.27745789 0.39582887 0.12081846 -0.02707434 0.18403857
[31] 0.04030607 0.25580415 -0.19203945 0.43584512 0.13395156 0.08477767
[37] -0.29279120 -0.33817701 0.14456618 0.13855812 -0.23983455 0.26144322
[43] -0.21206825 -0.70105524 0.38175014 0.23011048 0.21939444 0.34258334
[49] -0.27309788 0.43156387

Example 2

To find the mean of every n values in an R vector, use the command given below −

x2<-rnorm(1000)
colMeans(matrix(x2,10))

Output

If you execute the above given snippet, it generates the following output −

[1] 0.159658694 0.050333293 0.223651723 0.377602542 -0.108341204
[6] -0.089865167 -0.159113410 0.182136385 -0.152627157 -0.121909342
[11] -0.464853640 0.254499219 0.106476119 -0.230809171 -0.212390552
[16] 0.294346728 0.412278850 -0.369542646 -0.261966062 0.155860669
[21] -0.181901083 -0.304659719 -0.103341113 0.422466627 0.005477539
[26] -0.257441584 0.133207146 0.018217043 0.041766817 0.202315949
[31] 0.610907464 0.354076834 -0.034872693 0.522727723 -0.416864974
[36] -0.178608673 -0.153661849 -0.207162546 -0.291162800 0.480945300
[41] -0.326117823 0.116238987 0.072911755 0.077792420 -0.290771011
[46] -0.079132285 -0.205120462 0.504195280 -0.536198684 0.383896743
[51] 0.385278173 -0.152628128 -0.265470628 0.325611259 0.461375407
[56] 0.275344952 -0.010816585 -0.423987108 -0.132641782 -0.091155807
[61] 0.130903258 0.028381489 0.706176796 0.127173804 0.350620799
[66] -0.146080248 -0.327394613 -0.620314042 -0.010588524 0.220906668
[71] -0.235525232 -0.274529410 0.324540210 -0.224144203 -0.247653863
[76] 0.283131468 0.214888747 0.501703469 -0.304295747 0.168545666
[81] 0.356078979 0.014324885 0.349688549 -0.103445271 -0.015933839
[86] -0.556412735 0.078799954 -0.677425869 0.137469294 -0.196306711
[91] 0.022750679 0.585341629 0.232813646 -0.041987503 -0.454945609
[96] 0.303061096 0.250838047 -0.105794869 -0.084076707 0.048782881

Example 3

To find the mean of every n values in an R vector, use the command given below −

x3<-rpois(1000,8)
colMeans(matrix(x3,10))

Output

If you execute the above given snippet, it generates the following output −

[1] 6.7 10.6 9.1 8.3 8.4 6.6 8.7 7.3 7.4 8.3 7.1 7.0 7.9 9.9 6.5
[16] 8.6 8.5 6.9 7.6 7.6 8.2 8.3 8.0 7.0 9.8 7.6 8.5 7.3 7.8 9.1
[31] 6.6 8.6 7.8 8.1 8.0 8.8 8.1 8.3 8.6 6.8 9.9 8.9 8.7 9.4 7.4
[46] 8.0 8.6 6.9 6.9 8.0 7.5 6.8 8.2 6.8 6.9 8.7 8.1 8.3 7.0 7.9
[61] 8.3 9.9 8.2 8.0 8.4 8.5 8.2 8.2 8.0 7.7 9.1 7.4 7.5 7.1 7.4
[76] 8.0 7.1 6.8 6.0 6.7 7.9 7.7 8.7 6.9 7.1 8.0 8.3 7.0 8.5 7.8
[91] 7.9 8.1 5.4 7.1 8.5 7.9 7.4 8.4 7.4 9.4

Example 4

To find the mean of every n values in an R vector, use the command given below −

x4<-rpois(1000,10)
colMeans(matrix(x4,5))

Output

If you execute the above given snippet, it generates the following output −

[1] 10.0 11.2 9.2 10.8 6.8 10.2 9.2 13.2 8.4 10.2 8.6 8.2 8.6 11.2 8.2
[16] 12.0 10.2 10.2 8.2 8.6 10.0 10.8 9.4 10.0 10.0 10.0 8.8 11.0 8.0 10.6
[31] 10.0 10.0 8.4 11.0 10.0 8.6 12.0 7.0 9.8 10.8 10.6 11.0 13.4 12.6 12.0
[46] 12.0 10.2 8.6 11.4 9.4 9.0 12.4 11.4 7.8 11.2 9.2 9.6 9.0 10.6 7.8
[61] 10.4 6.6 8.8 10.0 11.2 11.0 8.8 10.8 10.0 10.6 10.8 7.8 10.0 12.8 9.6
[76] 8.6 10.2 7.6 11.2 10.4 10.6 7.8 11.0 11.0 10.2 7.4 10.0 11.0 8.6 11.8
[91] 9.6 10.0 11.8 10.2 8.6 9.6 9.0 8.4 10.4 13.2 9.0 8.8 11.4 11.6 10.4
[106] 8.6 10.2 12.0 7.2 11.8 8.8 10.0 7.2 9.6 9.8 11.6 9.8 11.4 11.4 8.4
[121] 12.6 9.2 7.8 9.0 10.0 10.2 8.6 9.8 7.8 7.8 9.0 10.2 9.0 9.4 9.6
[136] 10.6 10.8 11.0 8.8 9.4 9.6 10.6 8.2 10.4 9.2 10.4 10.0 6.4 11.4 13.2
[151] 8.6 10.6 11.2 8.0 9.2 9.8 8.6 10.2 10.4 7.6 11.0 7.6 8.2 10.2 10.6
[166] 12.0 8.4 9.8 10.8 11.4 9.8 12.0 10.6 9.4 8.4 10.4 9.8 10.8 10.6 8.6
[181] 9.0 9.2 9.8 11.4 12.6 11.0 12.0 8.8 7.2 13.2 9.2 10.4 7.8 11.2 10.6
[196] 9.0 9.2 9.0 9.0 9.6

Example 5

To find the mean of every n values in an R vector, use the command given below −

x5<-runif(1000,2,10)
colMeans(matrix(x5,10))

Output

If you execute the above given snippet, it generates the following output −

[1] 6.081613 6.538804 6.890214 5.520874 6.655614 5.339177 6.896096 7.146578
[9] 5.562699 7.058495 6.113300 6.193524 6.618599 5.667201 6.197804 7.176938
[17] 5.025604 6.181259 6.418706 6.210798 7.517997 5.661615 7.303842 5.474959
[25] 7.402129 6.361397 4.552639 5.676043 6.307914 5.746767 5.983984 6.566163
[33] 7.104588 5.940906 5.745185 6.670044 6.387342 5.634728 5.947384 6.386027
[41] 6.975309 4.971567 6.171601 6.106492 5.696239 5.962861 6.317535 5.297209
[49] 6.560181 6.175914 5.247441 7.524318 5.584901 6.379103 4.921781 5.316516
[57] 6.110755 6.757519 6.082820 5.418894 6.596565 6.206455 6.323560 7.292909
[65] 5.796476 5.606403 6.332172 5.728433 5.661947 4.566491 5.810569 5.761561
[73] 6.751246 6.728557 5.949663 5.672357 6.688222 6.231692 6.414203 6.972991
[81] 5.992874 5.960610 7.670685 4.765168 5.916204 6.003979 6.030567 5.366208
[89] 5.688393 5.185714 6.040857 5.051884 5.071893 6.970822 6.156001 6.030326
[97] 5.083717 4.550453 5.824033 5.622553

Example 6

To find the mean of every n values in an R vector, use the command given below −

x6<-round(rnorm(5000),2)
colMeans(matrix(x6,50))

Output

If you execute the above given snippet, it generates the following output −

[1] 0.0318 -0.0198 0.0266 -0.1298 0.0956 0.1450 -0.1556 -0.0914 -0.0040
[10] 0.2250 -0.0642 -0.0446 0.0216 -0.2722 -0.2198 0.0844 -0.1712 0.0952
[19] -0.0774 -0.1176 0.0432 0.1602 0.1646 0.0024 0.2524 -0.1700 0.1248
[28] -0.0752 0.1656 0.0440 0.0684 -0.1166 0.2474 -0.0596 0.0198 0.0882
[37] 0.1084 0.0656 -0.2086 0.1060 0.1344 -0.0450 -0.1928 -0.0142 -0.1564
[46] 0.1364 -0.1402 -0.2080 0.1072 0.2296 0.0862 0.2240 -0.0804 0.2690
[55] 0.1070 -0.0276 0.2410 -0.1416 -0.0270 -0.0668 -0.0084 -0.1834 0.1066
[64] 0.1382 -0.5326 0.1808 0.0258 -0.1994 -0.2214 -0.1854 0.0272 0.2252
[73] 0.1802 0.0314 0.1330 0.0810 -0.0308 -0.1608 0.0124 0.1008 0.0636
[82] 0.2316 0.1034 -0.0820 -0.1200 0.0588 -0.0650 0.0314 -0.0028 0.0416
[91] -0.0780 0.1056 -0.0650 0.0890 0.0752 -0.0532 -0.1098 0.0522 -0.2284
[100] 0.0502
raja
Updated on 11-Nov-2021 05:21:21

Advertisements