For this particular proble (counting), doesn't cumsum solve it
effectively and efficiently?
vv <- cumsum(v)
vv[n:length(vv)] - vv[1:(length(vv)-n+1]
Of course, this doesn't work for the general case of an arbitrary
sliding window function.
-s
On 12/15/08, Chris Oldmeadow <[email protected]> wrote:
> 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
>
> ______________________________________________
> [email protected] 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.
>
--
Sent from my mobile device
______________________________________________
[email protected] 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.