# [R] How to do a moving window on standard deviation

Joshua Ulrich josh.m.ulrich at gmail.com
Mon Jan 31 00:00:23 CET 2011

```On Sun, Jan 30, 2011 at 1:39 PM, eric <ericstrom at aol.com> wrote:
>
> I'd like to use vectorization to take a 4 point moving window on standard
> deviation on the close column and create another variable (st.dev) in the
> dataframe. Here's the dataframe
>
>
>        Date Close
> 1 2011-01-28 56.42
> 2 2011-01-27 57.37
> 3 2011-01-26 56.48
> 4 2011-01-25 56.39
> 5 2011-01-24 55.74
> 6 2011-01-21 55.46
>
> So the first 3 elements to the new st.dev column would be zero (c(rep(0,3)),

Technically, they should be NA because the standard deviation for
those periods is unknown, not zero.  Also, you may be introducing
element will contain future periods in the standard deviation
calculation).

> then the 4th element of the new std.dev column would be standard deviation
> of the first 4 closes. Next element would be sd of Close[5]:Close[1], then
> sd of Close[6]: Close[2] ...and so on until the last row of xyz.

Please see the help pages for the indexing (?"[") operator, the
sequence (?":") operator, and "Operator Syntax and Precedence"
doesn't work.

>
> There must be an easy vetorized way to do this but I don't see it. Sorry for
> the basic question but continuing to figure this new language out.
>
> Thanks in advance for the help
>

TTR::runSD will do what you want and the same call will work on
data.frame, matrix, vector, xts/zoo, or any other time-series object.
Note that time-series objects will generally put time periods in
ascending order.

library(TTR)
d\$st.dev <- runSD(d\$Close,4)
z <- zoo(d[,2,FALSE],as.Date(d[,1]))
z\$st.dev <- runSD(z\$Close,4)
x <- as.xts(z)
x\$st.dev <- runSD(x\$Close,4)

HTH,
--

> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-do-a-moving-window-on-standard-deviation-tp3247566p3247566.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help