Re: [R] sliding window over a large vector

2008-12-18 Thread Carl Witthoft
Because I had too much time on my hands, here's a little function that will do whatever you want over a window you specify. No, I haven't done any time trials :-( # my own boxcar tool, just because. # use bfunc to specify what function to apply to the windowed # region. boxcar<-function(x, wi

Re: [R] sliding window over a large vector

2008-12-16 Thread Whit Armstrong
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

Re: [R] sliding window over a large vector

2008-12-16 Thread Gabor Grothendieck
On Tue, Dec 16, 2008 at 8:23 AM, Gabor Grothendieck 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 1

Re: [R] sliding window over a large vector

2008-12-16 Thread Gabor Grothendieck
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) At any rate try this: library(zoo) 3 * rollmean(x, 3) On Mon, Dec 15, 2008 at 11:19 PM, Chris Oldmeadow wrote: > Hi all, > > I have a very large binary vector, I wish to calculate the num

Re: [R] sliding window over a large vector

2008-12-16 Thread Stavros Macrakis
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 wrote: >

Re: [R] sliding window over a large vector

2008-12-16 Thread Veslot Jacques
:markle...@verizon.net] >Envoyé : mardi 16 décembre 2008 10:25 >À : Veslot Jacques >Cc : Chris Oldmeadow; r-help@r-project.org >Objet : Re: [R] sliding window over a large vector > >Hi: Veslot: I'm too tired to even try to figure out why but I think >that there is s

Re: [R] sliding window over a large vector

2008-12-16 Thread c.oldmeadow
the function works for me s<-rbinom(1000,1,0.5) t<-slide(s,50) just too slow. Thanks. __ 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 p

Re: [R] sliding window over a large vector

2008-12-16 Thread Adrian Dusa
Hi Chris, On Tuesday 16 December 2008, Chris Oldmeadow wrote: > Hi all, > > I have a very large binary vector, I wish to calculate the number of > 1's over sliding windows. > [...snip...] Your function does not seem to function very well, could you please offer a self-contained, reproducible ex

Re: [R] sliding window over a large vector

2008-12-16 Thread markleeds
Hi: Veslot: I'm too tired to even try to figure out why but I think that there is something wrong with your sl function. see below for an empirical proof of that statement. OR maybe you're definition of sliding window is different than rollapply's definition but rollapply's answer makes more

Re: [R] sliding window over a large vector

2008-12-16 Thread Veslot Jacques
> sl <- function(x,z) c(0,cumsum(diff(x)[1:(length(x)-z-1)])) + > rep(sum(x[1:z]),length(x)-z) > x <- rbinom(10, 1, 0.5) > system.time(xx1 <- slide(x,12)) utilisateur système écoulé 36.860.45 37.32 > system.time(xx2 <- sl(x,12)) utilisateur système écoul

Re: [R] sliding window over a large vector

2008-12-16 Thread Dimitris Rizopoulos
you can have a look at the rollapply() function in the zoo package, e.g., x <- rbinom(100, 1, 0.5) z <- zoo(x) rollapply(z, 3, sum) I hope it helps. Best, Dimitris Chris Oldmeadow wrote: Hi all, I have a very large binary vector, I wish to calculate the number of 1's over sliding windows