Surprisingly Slow Standard Deviation in R

后端 未结 3 2100
死守一世寂寞
死守一世寂寞 2021-01-06 09:52

I am calculating standard deviations on an expanding window where at each point I recalculate the standard deviation. This seems like a fairly straightforward thing to do t

3条回答
  •  长情又很酷
    2021-01-06 09:59

    Edited to fix some typos, sorry.

    This takes ~1.3 seconds on my machine:

    t0 <- proc.time()[[3]]
    x <- rnorm(8000)
    z <- sapply(1000:8000,function(y){sd(x[seq_len(y)])})
    print(proc.time()[[3]]- t0)
    

    and I'd be willing to bet there are even faster ways of doing this. Avoid explicit for loops!

提交回复
热议问题