How do I compute the rolling standard deviation of a vector, such that
?
Example:
set.seed(123)
x <- c(NA, rnorm(5), NA, rnorm(5))
The result will return
sd(x[1:1],na.rm=T) = NA
sd(x[1:2],na.rm=T) = NA
sd(x[1:3],na.rm=T) = 0.2335561
sd(x[1:4],na.rm=T) = 1.140186
sd(x[1:5],na.rm=T) = 0.9355676
sd(x[1:6],na.rm=T) = 0.8110218
sd(x[1:7],na.rm=T) = 0.8110218
sd(x[1:8],na.rm=T) = 0.9550024
sd(x[1:9],na.rm=T) = 0.8718094
sd(x[1:10],na.rm=T) = 1.009344
sd(x[1:11],na.rm=T) = 0.9928846
sd(x[1:12],na.rm=T) = 0.9537841
You could use rollapplyr
from zoo
.
zoo::rollapplyr(x, seq_along(x), sd, na.rm = TRUE)
#[1] NA NA 0.234 1.140 0.936 0.811 0.811 0.955 0.872 1.009 0.993 0.954
Or a manual way in base R which would be slow than the zoo
approach.
sapply(seq_along(x), function(i) sd(x[1:i], na.rm = TRUE))