[R] How to get rid of loop?

Uwe Ligges ligges at statistik.tu-dortmund.de
Sun Apr 26 16:45:28 CEST 2009



Ken-JP wrote:
> set.seed(1)
> x <- runif(100)
> 
> # I want to calculate y such that:
> #
> # 1. if x>0.75, y <- 1
> # 2. else if x<0.25, y <- -1
> # 3. else if y_prev==1 && x<0.5, y <- 0
> # 4. else if y_prev==-1 && x>0.5, y <- 0
> # 5. else y <- y_prev
> #
> # 1. and 2. are directly doable without looping.
> #
> # How do I do 3.-5. without looping?  The problem is, I need to run this
> algorithm over gigs of data, so I
> # need to avoid looping, if at all possible...
> #
> # - Ken
> 
> 
> 


If y_prev is meant to be from a former iteration of a loop, you probably 
can't get rid of it. Original working code might have helped to 
udnertsand your problem better.
Anyway, perhaps you can imnprove your loop in other ways, but again, 
we'd need to see at least some code ....

Uwe Ligges




More information about the R-help mailing list