# How to calculate two period moving average for vector elements in R?

If we want to find the two−period moving average easily then it cannot be done in base R. We need to use rollmean function of zoo package that solves this problem in a single line of code. For example, if we have a vector x that contains hundred from starting from 1 to 100 then the two−period moving for x can be found by using rollmean(x,2)

library(zoo)

## Examples

Live Demo

x1<−1:10
x1

## Output

[1] 1 2 3 4 5 6 7 8 9 10

## Example

rollmean(x1,2)
[1] 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5
x2<−1:120
rollmean(x2,2)
[1]   1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5 11.5 12.5
[13] 13.5 14.5 15.5 16.5 17.5 18.5 19.5 20.5 21.5 22.5 23.5 24.5
[25] 25.5 26.5 27.5 28.5 29.5 30.5 31.5 32.5 33.5 34.5 35.5 36.5
[37] 37.5 38.5 39.5 40.5 41.5 42.5 43.5 44.5 45.5 46.5 47.5 48.5
[49] 49.5 50.5 51.5 52.5 53.5 54.5 55.5 56.5 57.5 58.5 59.5 60.5
[61] 61.5 62.5 63.5 64.5 65.5 66.5 67.5 68.5 69.5 70.5 71.5 72.5
[73] 73.5 74.5 75.5 76.5 77.5 78.5 79.5 80.5 81.5 82.5 83.5 84.5
[85] 85.5 86.5 87.5 88.5 89.5 90.5 91.5 92.5 93.5 94.5 95.5 96.5
[97] 97.5 98.5 99.5 100.5 101.5 102.5 103.5 104.5 105.5 106.5 107.5 108.5
[109] 109.5 110.5 111.5 112.5 113.5 114.5 115.5 116.5 117.5 118.5 119.5
x3<−sample(0:5,111,replace=TRUE)
rollmean(x3,2)
[1]  3.0 4.5 5.0 4.5 2.5 1.0 2.0 1.5 0.5 2.5 4.0 2.0 0.0 0.5 1.0 3.0 3.0 2.0
[19] 3.0 3.0 1.5 0.5 3.0 2.5 1.0 2.0 3.5 5.0 3.0 2.0 2.5 3.5 4.0 1.5 0.5 2.5
[37] 4.5 3.5 1.5 1.0 2.0 3.0 2.5 1.5 1.5 2.5 3.0 2.0 1.5 2.5 3.0 2.5 2.0 2.5
[55] 2.0 2.5 3.5 2.5 2.0 1.5 2.0 2.5 1.5 1.5 1.5 2.0 3.5 2.5 1.0 2.0 4.0 5.0
[73] 4.5 4.0 4.5 4.5 4.0 2.5 2.5 4.5 4.0 2.5 1.0 2.5 2.5 0.5 1.0 1.0 1.5 2.5
[91] 3.0 1.5 0.5 1.0 1.5 3.0 3.5 2.0 1.0 2.5 3.5 3.0 2.5 3.5 5.0 5.0 4.5 2.5
[109] 3.0 2.5
x4<−sample(1:5,111,replace=TRUE)
rollmean(x4,2)
[1]  5.0 3.0 2.5 4.0 2.5 2.5 3.0 1.5 3.0 4.0 2.0 2.5 2.5 2.0 4.0 5.0 4.0 3.0
[19] 4.0 3.0 2.0 3.0 4.0 4.5 3.5 3.5 2.5 1.0 3.0 4.5 4.0 3.0 1.5 1.0 3.0 5.0
[37] 3.0 1.0 1.5 2.5 2.5 3.0 2.5 2.0 3.5 3.5 3.5 3.5 2.0 2.0 4.0 4.0 4.0 5.0
[55] 3.5 3.0 3.0 2.0 3.0 2.5 1.5 1.5 3.0 3.0 1.5 2.0 2.5 4.0 5.0 4.5 3.0 1.5
[73] 2.0 4.0 3.5 2.5 2.0 2.0 3.5 4.0 4.0 4.0 3.0 2.0 2.5 4.0 4.5 2.5 1.5 2.5
[91] 2.5 3.5 3.0 1.0 1.5 2.0 2.5 2.0 1.0 2.5 2.5 3.0 4.5 3.5 4.0 5.0 3.5 2.0
[109] 3.0 4.0
x5<−sample(1:50,120,replace=TRUE)
rollmean(x5,2)
[1]  37.5 32.0 32.5 34.5 29.0 34.0 30.0 33.0 30.0 30.0 34.5 13.5 13.0 16.0 27.0
[16] 29.0 28.5 35.5 37.5 34.5 29.0 28.0 31.0 43.5 46.0 27.5 16.5 32.5 33.5 32.5
[31] 37.5 31.0 15.5 10.5 26.0 21.5 21.0 30.0 27.5 21.0 27.5 37.0 33.0 43.0 38.5
[46] 21.0 20.0 27.0 25.5 33.5 34.5 28.0 16.5 10.5 26.5 24.0 25.0 32.0 16.5 21.0
[61] 20.5 13.0 25.0 35.5 23.0 14.5 21.5 10.5 18.0 17.5 7.5 11.0 10.5 27.5 23.0
[76] 13.5 15.5 21.5 35.0 38.0 29.5 21.0 17.5 25.0 22.0 14.0 18.0 13.5 12.5 29.5
[91] 47.0 27.0 26.5 43.0 45.5 39.5 33.5 30.5 16.0 5.0 26.5 42.0 28.5 24.5 21.0
[106] 8.5 6.5 27.5 35.5 25.5 33.5 39.0 31.0 26.5 32.5 21.0 10.5 11.5 13.0
x6<−sample(91:100,120,replace=TRUE)
rollmean(x6,2)
[1]  95.0 94.0 95.0 94.5 96.5 95.0 92.5 95.0 96.5 97.0 96.0 97.0
[13] 98.0 96.5 98.0 99.5 99.5 97.5 94.5 97.0 99.5 97.0 93.5 95.5
[25] 99.5 98.0 96.0 98.0 97.0 92.5 95.5 100.0 100.0 99.0 99.0 99.5
[37] 97.5 94.5 96.5 98.0 93.5 95.0 97.0 93.5 95.0 97.0 95.5 93.0
[49] 92.0 93.0 96.0 97.0 96.0 97.0 94.0 94.5 98.0 98.5 95.0 92.5
[61] 95.0 94.5 96.0 96.0 95.5 97.5 97.0 96.0 94.5 93.0 91.5 92.0
[73] 95.0 97.5 97.5 95.0 95.0 97.0 95.5 95.0 93.5 94.0 94.0 93.5
[85] 96.0 94.5 95.0 97.5 99.0 95.5 95.5 95.5 95.5 96.5 92.5 95.0
[97] 94.5 95.0 95.5 92.0 91.5 93.5 95.0 95.5 96.5 96.5 94.5 93.5
[109] 97.0 95.0 92.5 92.5 91.5 94.0 95.0 95.5 98.5 96.0 95.5
x7<−rnorm(50,5,0.5)
rollmean(x7,2)
[1] 5.480386 5.447735 5.411541 4.920386 5.179600 5.283988 5.171337 5.249699
[9] 5.191743 5.259015 5.007683 4.996620 4.829728 4.294174 4.718041 5.287286
[17] 5.300202 5.306356 5.328401 5.122863 5.064679 4.806393 4.620227 4.946723
[25] 5.273818 5.262808 5.224571 5.105804 5.115384 5.720728 4.859122 4.447057
[33] 5.122608 4.862555 4.868280 4.399689 4.053471 4.768004 4.767681 5.001960
[41] 5.081844 4.530953 4.181293 3.973446 4.100880 4.184364 4.364118 4.598242
[49] 4.764706
x8<−runif(50,5,10)
rollmean(x8,2)
[1]  8.058824 8.243607 8.555279 8.167787 7.318688 8.031011 8.259745 7.551773
[9]  6.461240 7.416219 9.041726 7.248550 6.468459 7.297782 7.023565 6.675229
[17] 6.787259 7.667929 8.520316 7.144133 7.185223 7.089798 7.531991 8.460906
[25] 8.486284 8.251683 6.947352 7.645662 6.853478 6.889086 7.891153 8.025121
[33] 7.420661 7.357484 6.920305 5.854457 6.961225 8.196131 8.013994 7.056246
[41] 8.132790 8.148006 8.128420 7.191846 7.159202 9.603268 8.572502 7.001365
[49] 6.500651
x9<−rexp(50,2.1)
rollmean(x9,2)
[1]  0.50955472 0.47625696 0.44801966 0.54031908 0.44315428 0.30659278
[7]  0.12889859 0.06259362 0.60524099 0.59404019 0.20083159 0.26323887
[13] 0.17771971 0.80555530 0.76444119 0.87520681 0.97853392 0.58382596
[19] 0.48346306 0.13819934 0.15771218 0.24265242 0.39860430 0.72997709
[25] 0.55388338 0.07809430 0.10359228 0.17891867 0.40828716 0.33985932
[31] 0.21839668 0.40999751 0.33016602 0.14308831 0.63506390 1.05480764
[37] 1.29507319 1.23082710 0.59804633 0.39464417 0.31223577 0.14141055
[43] 0.04557646 0.29261665 0.61306151 0.55907498 0.46214800 0.73766138
[49] 0.55396346
x10<−rlnorm(50,2,1.5)
rollmean(x10,2)
[1]   4.871013 5.354362 1.862284 31.728599 31.585093 1.676455 15.009501
[8]  17.861997 6.556540 37.194718 34.591386 6.580697 10.248382 37.585671
[15] 34.945119 8.400405 13.329515 6.899100 17.023316 24.452512 53.928439
[22] 48.656265 2.999969 4.237573 7.059769 4.747940 4.274969 4.085395
[29] 3.502757 6.556758 18.719148 19.074384 7.633016 10.052852 80.373963
[36] 85.713024 18.683646 7.845827 4.430249 11.650022 26.635095 18.275929
[43] 1.449896 2.641906 4.318882 8.366553 9.616981 19.477526 17.515400

Updated on: 17-Oct-2020

100 Views