# [R] sliding window over a large vector

Whit Armstrong armstrong.whit at gmail.com
Tue Dec 16 16:20:45 CET 2008

```if you want the speed, you can simply build an fts time series from
it, then apply the moving.sum function and throw away the dates.

this will probably be the fastest implementation of rolling applies
out there unless you do a cumsum difference function.

I got a sample timing of 2 seconds on 12m length vector (see botttom of email).

library(fts)

your.data <- c(0,1,1,0,1,1,1,0,0,0,0,1,1,1,1)

## dates generated automatically
fake.fts <- fts(data=your.data)

## throw away dates

my timing:
> library(fts)
> big.fts <- fts(data=rep(1,12000000))
> system.time(ans.fts <- moving.sum(big.fts,20))
user  system elapsed
1.970   0.081   2.051
> nrow(big.fts)
[1] 12000000
> nrow(ans.fts)
[1] 11999981
>

-Whit

On Tue, Dec 16, 2008 at 9:12 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> On Tue, Dec 16, 2008 at 8:23 AM, Gabor Grothendieck
> <ggrothendieck at gmail.com> wrote:
>> There seems to be something wrong:
>>
>>> slide(c(1, 1, 0, 1), 2)
>> [1] 2 2
>>
>> but the output should be c(2, 1, 2)
>
> That should be c(2, 1, 1)
>
>>
>> At any rate try this:
>>
>> library(zoo)
>> 3 * rollmean(x, 3)
>>
>>
>> On Mon, Dec 15, 2008 at 11:19 PM, Chris Oldmeadow
>>> 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
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help