I am looking for the fastest way to calculate a moving amount with window resizing. I am using the following code, but for vectors of length 1M it is too slow.
thank
set.seed(1)
n = 10L
x = runif(n)
window = pmin(sample(1:10, n, TRUE), n:1-1)
s = function(x, w){
n = length(x)
out = rep(NA, n)
for(i in 1:n){
k = w[i]
out[i] = sum(x[i:(i+k)])
}
out
}
s(x, window)
source
share