[R] Conditionally incrementing a loop counter

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Dec 27 23:22:41 CET 2007


Mike Jones wrote:
> Hi,
> I am trying a for loop from 1 to 100 by 1. However, if a condition does
> not get met, I want to "throw away" that iteration. So if my loop is
> for (i in 1:100)
> and i is say, 25 and the condition is not met then I don't want i to go
> up to 26.  Is there a way to do that? I can't seem to manually adjust i
> because from what I understand, R creates 100 long vector and uses that
> to "loops thru" and I'm not sure how to get at the index of that vector.
> Any suggestions? Thanks in advance.
>   
You're not being entirely clear. If you don't increment the loop 
counter, you generally get stuck in an infinite loop. That is, unless 
you rely on external input somehow, which you never told us about. You 
can easily do a while()-type loop in R, but whether that solves your 
problem is hard to tell.

Often the solution lies in whole-object thinking, and can end up quite 
different from index-fiddling, so it might help if you said what the 
actual problem is.

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list