Fast way to count events over a sliding window

Viewed 1777

Suppose I have x = rnorm(100000) and instead of doing a 1000 length sliding window moving average, I wanted to do a 1000 length sliding window that counts all the times that x is above 0.2.

For example,

x <- rnorm(1004)
start <- 1:1000
record <- list()
while(start[length(start)] <= length(x)) {
    record[[length(record) + 1]] <- length(which(x[start] > 0.2))/length(start)
    start <- start + 1
    print(record[[length(record)]]);flush.console()
}

This becomes unmanegable for large length(x). What is a highly efficient method?

3 Answers
Related