[R] Conditionally incrementing a loop counter: Take 2

Peter Dalgaard p.dalgaard at biostat.ku.dk
Fri Dec 28 02:19:31 CET 2007


John Fox wrote:
> Dear Mike,
>
> You could use a repeat loop and manage the index yourself:
>
> i <- 0
> repeat{
> 	x <- runif(1)
> 	if (x < .1){
> 	   i <- i + 1
> 	   cat("x = ", x, "\n")
> 	   }
>     if (i == 10) break
>     }
>
> But if your example problem reflects your actual application, why not just
> generate uniform random numbers on the interval (0, .1)?
>   

As I read it, it is the sequence of i's not the x's that are sought. The 
example suddenly skipped from something with x > .7 to something with x 
< .1, but it might be that Mike's while loop just needed to print i at 
the top of the loop rather than at the bottom. The result should 
(AFAICS) be equivalent to

 > rep(1:10, 1+rgeom(10, .7))
 [1]  1  1  2  3  4  5  6  7  8  8  9 10 10


> I hope this helps,
>  John
>
> --------------------------------
> John Fox, Professor
> Department of Sociology
> McMaster University
> Hamilton, Ontario, Canada L8S 4M4
> 905-525-9140x23604
> http://socserv.mcmaster.ca/jfox
>
>
>   
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
>> project.org] On Behalf Of Mike Jones
>> Sent: December-27-07 6:08 PM
>> To: Peter Dalgaard
>> Cc: r-help at stat.math.ethz.ch
>> Subject: Re: [R] Conditionally incrementing a loop counter: Take 2
>>
>> Since I didn't want the i to increment in the loop when the condition
>> is not met, then in my example I wanted the loop to actually run 14
>> times instead of the 10 since I wanted 4 of the iterations to be thrown
>> away, or ignored.  I still haven't been able to figure this out.  Going
>> the "while" route doesn't seem to work for me either.
>>
>>
>> nums <- numeric(10)
>> i <- 1
>> garbage <- 0
>>
>> while (i <= 10){
>> 	x <- runif(1)
>> 	cat("x = ",x,"\n")
>> 	if (x < 0.1){
>> 		nums[i] <- x
>> 		i <- i + 1
>> 	}
>> 	else{
>> 	        garbage <- garbage+1
>> 	}
>> cat("i = ",i,"garbage = ",garbage,"\n")
>> }
>>
>> -----Original Message-----
>> From: Peter Dalgaard [mailto:p.dalgaard at biostat.ku.dk]
>> Sent: Thursday, December 27, 2007 5:36 PM
>> To: Mike Jones
>> Cc: r-help at stat.math.ethz.ch
>> Subject: Re: [R] Conditionally incrementing a loop counter: Take 2
>>
>>
>> Mike Jones wrote:
>>     
>>> 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
>>>
>>>
>>>       
>> Is this the kind of effect you want?
>>
>>  > x <- runif(10)
>>  > cbind(x, 1:10, cumsum(x < .7))
>>                 x
>>  [1,] 0.384165631  1 1
>>  [2,] 0.392715845  2 2
>>  [3,] 0.895936431  3 2
>>  [4,] 0.910242185  4 2
>>  [5,] 0.689987301  5 3
>>  [6,] 0.237071326  6 4
>>  [7,] 0.225032680  7 5
>>  [8,] 0.001856286  8 6
>>  [9,] 0.392034868  9 7
>> [10,] 0.655076045 10 8
>>
>> If you insist on using a loop, you need to separate the loop control
>> from the manipulation of i, as in (e.g.)
>>
>> i <- 0
>> for (j in 1:10){
>>    i <- i + 1
>>    cat("initial i = ",i,"\n")
>>    x <- runif(1)
>>    if (x > 0.7){
>>       i <- i-1
>>    }
>>    cat("second i = ",i,"\n")
>> }
>>
>>
>>     
>>>>  -----Original Message-----
>>>> From: 	Mike Jones
>>>> Sent:	Thursday, December 27, 2007 4:35 PM
>>>> To:	'r-help at 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.
>>>
>>>       
>> --
>>    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
>>
>> ______________________________________________
>> 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.
>>     
>
>   


-- 
   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