Note that while those other routines are quick, your original code
can easily be made much faster for large n (number of rows) by
preallocating the output vectors to their ultimate length.
E.g., replace
out <- numeric()
elements <- numeric()
for (i in 1:(length(data[, 1]) - windowSize + 1)) {
by
nOut <- length(data[, 1]) - windowSize + 1
out <- numeric(nOut)
elements <- numeric(nOut)
for (i in 1:nOut) {
The former's time is quadratic in n and the latter's is linear.
(If what you asked about, running means of constant length, is your
real problem and not just a simplified version, then base::filter
can get it done quickly also. Also, it is a waste to compute
length(i:(i+windowSize-1)) even once, not to speak of computing it for
every i)
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at
r-project.org] On Behalf Of Alaios
> Sent: Saturday, March 03, 2012 2:30 PM
> To: R help
> Subject: [R] Sliding Window in R (solved)
>
> Dear all,
> you can find below my solution for sliding a window. Please find below the
code for the two
> alternatives and the benchmarks.
>
>
> install.packages('caTools')
> require(caTools)
> do_sliding_for_a_window_duty_cycle <- function(DataToAnalyse,?
windowSize) {
>
>
> ? data<-DataToAnalyse
> ? out <- numeric()
> ? elements<- numeric()
> ? if (length(data[,1]) >= windowSize){
> ????? for (i in 1:(length(data[,1]) - windowSize +1? )) {
> ??????? out[i] <- mean(data[i:(i + windowSize - 1), ])
> ??????? elements[i]<-length(i:(i + windowSize - 1))
> ????? }
> ? }
>
> ? return (list(result=out , numberOfElements=elements,
windowSize=windowSize ))
> }
>
>
> do_sliding_for_a_window_duty_cycle_alternative <-
function(DataToAnalyse,
> windowSize) {
> ??? result=
runmean(rowMeans(DataToAnalyse),windowSize,endrule="trim",alg="C")
>
> ??? return( list(result= result, windowSize=windowSize))
> }
>
>
>
>
DataToAnalyse<-matrix(data=round(seq(1:100000000)),nrow=10000,byrow=TRUE)
>
>
>
> # How much time they need to runmean
>
>
>
system.time(a<-do_sliding_for_a_window_duty_cycle_alternative(DataToAnalyse,500))
>
> system.time(b<-do_sliding_for_a_window_duty_cycle(DataToAnalyse,500))
>
>
>
> # Are the results the same
>
> # Print the difference of the resulted length.
> print(length(a$result)-length(b$result))
>
> # Difference of the returned values
> boxplot(a$result-b$result)
>
>
>
>
>
>
> My results on a normal dual core laptop cpu
>
>
system.time(a<-do_sliding_for_a_window_duty_cycle_alternative(DataToAnalyse,500))
> ?? user? system elapsed
> ? 0.352?? 0.004?? 0.368
>
> ?system.time(b<-do_sliding_for_a_window_duty_cycle(DataToAnalyse,500))
> ??? user?? system? elapsed
> 2834.009? 207.989 3080.434
>
>
> > print(length(a$result)-length(b$result))
> [1] 0
>
> [[alternative HTML version deleted]]