Re: [R] Conditionally incrementing a loop counter

From: Peter Dalgaard <>
Date: Thu, 27 Dec 2007 23:22:41 +0100

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
~~~~~~~~~~ - (                  FAX: (+45) 35327907

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Thu 27 Dec 2007 - 22:25:34 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 27 Dec 2007 - 22:30:20 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.