Skip to content
Prev 164876 / 398503 Next

sliding window over a large vector

Hi,

I just wrote a function quicker than slide() function with the same output, but I don't know what to do with this function!
[1] 1 1 2 2 1 2 2 2 2 1 1 1 2 1 2 1 2 2 1 2 2
+    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)
+ }
[1] 1 1 2 2 1 2 2 2 2 1 1 1 2 1 2 1 2 2 1 2 2
[1] 1 1 2 2 1 2 2 2 2 1 1 1 2 1 2 1 2 2 1 2 2
utilisateur     syst?me      ?coul? 
      36.86        0.45       37.32
utilisateur     syst?me      ?coul? 
       0.01        0.00        0.02
[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