[R] Conditionally incrementing a loop counter: Take 2

From: Mike Jones <MikeJones_at_westat.com>
Date: Thu, 27 Dec 2007 17:20:23 -0500


My apologies for not including a working example.

Here it is:

for (i in 1:10){

   cat("initial i = ",i,"\n")
   x <- runif(1)
   if (x > 0.7){

      i <- i-1
   }
   cat("second i = ",i,"\n")
}

When I ran this i got what follows, so there were four cases where I wanted the i not to increment.

initial i = 1
second i = 1
initial i = 2
second i = 1
initial i = 3
second i = 3
initial i = 4
second i = 3
initial i = 5

second i = 4
initial i = 6
second i = 6
initial i = 7
second i = 7
initial i = 8
second i = 7
initial i = 9
second i = 9
initial i = 10
second i = 10

> -----Original Message-----
> From: Mike Jones
> Sent: Thursday, December 27, 2007 4:35 PM
> To: 'r-help@lists.R-project.org'
> Subject: Conditionally incrementing a loop counter
>
> Hi,
> I am trying a for loop from 1 to 10 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:10)
> and i is say, 4 and the condition is not met then I don't want i to go
> up to 5. Is there a way to do that? I can't seem to manually adjust i
> because from what I understand, R creates 10 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.
>

>
>
>
>
>
>
>
>
>
> Mike Jones
> Westat
> 1650 Research Blvd. RE401
> Rockville, MD 20850
> Ph: 240.314.2312
>

        [[alternative HTML version deleted]]



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 27 Dec 2007 - 22:23:40 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 Fri 28 Dec 2007 - 02:30:20 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.