[R] vectorisation?

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Mon Jul 26 10:13:00 CEST 2010


have a look at function rollapply() from package zoo.

I hope it helps.

Best,
Dimitris


On 7/26/2010 8:28 AM, Raghu wrote:
> Hi
>
> I have 3500 rows of data (say a single column) in a vector. If I want to
> compare every ith element with the simple average of the previous (i-5)
> elements, then I could write something like this:
>
> for(i in 6:NROW(data)){
>         if(data[i] =>  mean(data[(i-5):i]) (counter[i]=1)}
> }
>
> Is it possible to replicate the above faster in R?
>
>

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014



More information about the R-help mailing list