How to do a moving window on standard deviation
How about considering 'embed':
d
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
x <- embed(d$Close, 4) x
[,1] [,2] [,3] [,4] [1,] 56.39 56.48 57.37 56.42 [2,] 55.74 56.39 56.48 57.37 [3,] 55.46 55.74 56.39 56.48
apply(x, 1, sd)
[1] 0.4714870 0.6700497 0.4968149
On Sun, Jan 30, 2011 at 2: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 head(xyz) ? ? ? ?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)), 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. 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 -- 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 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve?