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]]
______________________________________________ R-help@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.