r - Calculate cumulative standard deviation -
this question has answer here:
i'm trying calculate standard deviation of values in time series, i'd incrementally advancing 1 day initial date value each time. know there way in r (probably using ddply?) doesn't involve nasty for-loop. help!
d<-seq(from=as.date("2013-01-01"), to=as.date("2013-02-01"), by="day") v <-rnorm(32, 10, 5) test.df<-data.frame(the_date=d, value=v)
here's way i'm doing now.
result <- c() for(i in 2:nrow(test.df)){ result[i-1] <- sd(test.df[1:i,]$value)}
use ttr::runsd
cumulative=true
.
library(ttr) x <- xts(test.df[,2],test.df[,1]) runsd(x, n=1, cumulative=true)
Comments
Post a Comment