Skip to content
Back to formatted view

Raw Message

Message-ID: <13E5504DD4AE454AAD151A4D239FBE5E010B00DA@parpaioun.aix.cemagref.fr>
Date: 2008-12-16T08:47:34Z
From: Veslot Jacques
Subject: sliding window over a large vector
In-Reply-To: <49472C6B.5000704@student.qut.edu.au>

> sl <- function(x,z) c(0,cumsum(diff(x)[1:(length(x)-z-1)])) + rep(sum(x[1:z]),length(x)-z)
> x <- rbinom(100000, 1, 0.5)
> system.time(xx1 <- slide(x,12))
utilisateur     syst?me      ?coul? 
      36.86        0.45       37.32 
> system.time(xx2 <- sl(x,12))
utilisateur     syst?me      ?coul? 
       0.01        0.00        0.02 
> all.equal(xx1,xx2)
[1] TRUE

Jacques VESLOT

CEMAGREF - UR Hydrobiologie

Route de C?zanne - CS 40061      
13182 AIX-EN-PROVENCE Cedex 5, France

T?l.   + 0033   04 42 66 99 76
fax    + 0033   04 42 66 99 34
email   jacques.veslot at cemagref.fr  


>-----Message d'origine-----
>De?: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] De la part
>de Chris Oldmeadow
>Envoy??: mardi 16 d?cembre 2008 05:20
>??: r-help at r-project.org
>Objet?: [R] sliding window over a large vector
>
>Hi all,
>
>I have a very large binary vector, I wish to calculate the number of
>1's  over sliding windows.
>
>this is my very slow function
>
>slide<-function(seq,window){
>   n<-length(seq)-window
>   tot<-c()
>   tot[1]<-sum(seq[1:window])
>   for (i in 2:n) {
>      tot[i]<- tot[i-1]-seq[i-1]+seq[i]
>   }
>   return(tot)
>}
>
>this works well for for reasonably sized vectors. Does anybody know a
>way for large vectors ( length=12 million), im trying to avoid using C.
>
>Thanks,
>Chris
>
>______________________________________________
>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.