[R] Sliding a Window in R
Berend Hasselman
bhh at xs4all.nl
Sat Mar 3 14:55:01 CET 2012
On 03-03-2012, at 14:31, Alaios wrote:
> Dear all,
> I am having a vector of around 300.000 elements and I Want to slide fast a window from the first element until the last-Windowsize
>
> what I have so far is the following for statement:
>
> for (i in 1:(length(data[,1]) - windowSize)) {
> out[i] <- mean(data[i:(i + windowSize - 1), ])
> elements[i]<-length(i:(i + windowSize - 1))
> }
>
> but this of course takes ages to run, especially with small window sizes!.
> Is it possible to speed up this in many cores in R? If yes how?
Try this
library(zoo)
out <- rowSums(rollmean(data,windowSize))/windowSize
I don't think you need elements since all entries have the same size.
Berend
More information about the R-help
mailing list