[R] for loop

Ray Brownrigg Ray.Brownrigg at ecs.vuw.ac.nz
Mon Sep 28 23:49:36 CEST 2009


On Tue, 29 Sep 2009, Antonio Paredes wrote:
> Can somebody give a hint on how to speed-up the following loop:
>
>
> for(j in 0:KM1)
>   {
>    k=j*60
>    for(i in 1:60)
>    {
>   dat$yvac[k+i]= rbinom(1,dat$nvac[k+i],dat$p.trt[j+i])
>   }
>  }
>
> K1=999

How about:

rbinom((KM1 + 1)*60, dat$nvac, dat$p.trt[rep(0:KM1, each=60) + 1:60])

HTH
Ray Brownrigg




More information about the R-help mailing list