Skip to content
Prev 313777 / 398506 Next

calculate a "rolling mean" including standard deviation

Hello,

Something like this?


d <- myframe2$distance
n <- length(d)
mu <- rep(NA, n - 1)
mu[n - 1] <- m <- mean(d[(n - 1):n])
s <- sd(d[(n - 1):n])
ex <- 0
for(i in rev(seq_len(n))[-(1:2)]){
     if(d[i] < m + s){
         ex <- 0
         mu[i] <- m
     }else{
         ex <- ex + 1
         if(ex >= 2) break
     }
     m <- mean(d[i:n])
     s <- sd(d[i:n])
}
mu


Hope this helps,

Rui Barradas
Em 17-12-2012 14:55, Tagmarie escreveu: