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? I would like to thank you in advance for your help B.R Alex [[alternative HTML version deleted]]
R. Michael Weylandt <michael.weylandt@gmail.com>
2012-Mar-03 13:36 UTC
[R] Sliding a Window in R
Just move it to C and you'll probably be ok. I believe runmean in library(caTools) provides a very fast implementation. Michael On Mar 3, 2012, at 8:31 AM, Alaios <alaios at yahoo.com> 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? > > I would like to thank you in advance for your help > > B.R > Alex > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On Sat, Mar 3, 2012 at 8:31 AM, Alaios <alaios at yahoo.com> 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? > > I would like to thank you in advance for your help >rollmean in the zoo package is quite fast. There are also a variety of other rolling routines there, principally rollapply which is very flexible but, of course, is much slower. Please provide complete self-contained reproducible code next time as per last two lines of every message in r-help. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
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
On 03-03-2012, at 14:55, Berend Hasselman wrote:> > 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))/windowSizeCorrection: out <- rowSums(rollmean(data,windowSize))/ <number of columns in data> Berend