How to find the moving standard deviation in an R data frame?


To find the moving standard deviation in an R data frame, we can make use of rollapply function of zoo package.

For example, if we have a data frame called df and we want to find the 2 moving standard deviations then we can use the below given command −

rollapply(df,width=2,FUN=sd,fill=0,align="r")

Example 1

Following snippet creates a sample data frame −

x1<-rnorm(20)
x2<-rnorm(20)
x3<-rnorm(20)
df1<-data.frame(x1,x2,x3)
df1

The following dataframe is created −

        x1           x2         x3
1  -0.049371233   0.15042948   0.7801880
2  -2.309740485   0.16439819  -0.5155871
3   0.275820922  -0.34764174   1.4993257
4  -0.920297787   0.25222530   1.5681985
5   0.110624467   0.01890216   1.2107487
6   0.865938984  -1.67177878   1.1043152
7   0.253139069   1.32182287   0.5327715
8   1.414675759  -2.65619112  -1.0617283
9   0.416841760  -0.39158788  -0.3602582
10 -0.358835803   0.20922777   1.2902007
11 -0.455361301  -1.40088912   2.0602088
12  0.742073720  -0.42320099  -0.8943533
13 -0.002643386   0.75774583   0.7258981
14 -1.967374060  -1.92858217   0.8854961
15 -1.741133384  -1.58046760  -1.5376346
16 -1.828572667  -0.54706762  -0.2381593
17  0.674338272  -0.17540436  -0.7148876
18 -0.437785459   0.36396586  -1.2007988
19 -0.235525784  -0.39931221  -0.9674260
20  0.141622142   0.03842005   0.4136045

To load zoo package and find 5-moving standard deviation for data in df1, add the following code to the above snippet −

library(zoo)
rollapply(df1,width=5,FUN=sd,fill=0,align="r")

Output

If you execute all the above given snippets as a single program, it generates the following output −

          x1         x2      x3
[1,]  0.0000000  0.0000000  0.0000000
[2,]  0.0000000  0.0000000  0.0000000
[3,]  0.0000000  0.0000000  0.0000000
[4,]  0.0000000  0.0000000  0.0000000
[5,]  1.0722352  0.2361815  0.8544178
[6,]  1.2487785  0.7913527  0.8545377
[7,]  0.6482081  1.0828798  0.4117639
[8,]  0.8774868  1.5941549  1.0374862
[9,]  0.5308056  1.5384901  0.9772044
[10,] 0.6660168  1.5621470  0.9964741
[11,] 0.7504078  1.5214858  1.2488476
[12,] 0.7814668  1.1231666  1.3921332
[13,] 0.5092032  0.8063859  1.2013811
[14,] 0.9903649  1.1098367  1.0852588
[15,] 1.1526908  1.0892921  1.4502026
[16,] 1.2442845  1.0606422  1.0377557
[17,] 1.2212830  1.0849092  1.0110028
[18,] 1.1481566  0.9602383  0.9464956
[19,] 1.0641887  0.7117466  0.4921518
[20,] 0.9350305  0.3605345  0.6423378

Example 2

Following snippet creates a sample data frame −

y1<-rpois(20,1)
y2<-rpois(20,5)
y3<-rpois(20,2)
y4<-rpois(20,5)
df2<-data.frame(y1,y2,y3,y4)
df2

The following dataframe is created −

   y1 y2 y3 y4
1  2  4  1  7
2  0  6  0  5
3  1  7  4  7
4  0  6  4  5
5  1  3  2  6
6  0  2  1  9
7  1  7  2  5
8  2  4  4  1
9  0  5  4  2
10 1  6  2  6
11 0  7  0  4
12 1  2  1  7
13 0  6  2  8
14 1  6  4  1
15 1  3  1  6
16 6  6  5  2
17 1  8  5  3
18 2  5  1  7
19 1  5  1  6
20 0  6  1  4

To find 4-moving standard deviation for data in df2, add the following code to the above snippet −

rollapply(df2,width=4,FUN=sd,fill=0,align="r")

Output

If you execute all the above given snippets as a single program, it generates the following output −

         y1         y2         y3       y4
[1,]  0.0000000  0.000000  0.0000000  0.0000000
[2,]  0.0000000  0.000000  0.0000000  0.0000000
[3,]  0.0000000  0.000000  0.0000000  0.0000000
[4,]  0.9574271  1.258306  2.0615528  1.1547005
[5,]  0.5773503  1.732051  1.9148542  0.9574271
[6,]  0.5773503  2.380476  1.5000000  1.7078251
[7,]  0.5773503  2.380476  1.2583057  1.8929694
[8,]  0.8164966  2.160247  1.2583057  3.3040379
[9,]  0.9574271  2.081666  1.5000000  3.5939764
[10,] 0.8164966  1.290994  1.1547005  2.3804761
[11,] 0.9574271  1.290994  1.9148542  2.2173558
[12,] 0.5773503  2.160247  1.7078251  2.2173558
[13,] 0.5773503  2.217356  0.9574271  1.7078251
[14,] 0.5773503  2.217356  1.7078251  3.1622777
[15,] 0.5000000  2.061553  1.4142136  3.1091264
[16,] 2.7080128  1.500000  1.8257419  3.3040379
[17,] 2.5000000  2.061553  1.8929694  2.1602469
[18,] 2.3804761  2.081666  2.3094011  2.3804761
[19,] 2.3804761  1.414214  2.3094011  2.3804761
[20,] 0.8164966  1.414214  2.0000000  1.8257419

Updated on: 11-Nov-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements