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. Mike Jones Westat 1650 Research Blvd. RE401 Rockville, MD 20850 Ph: 240.314.2312 [[alternative HTML version deleted]]
Please read the posting guide and provide a simple reproducible example as it asks you to. Very likely a loop is not even needed. -- Bert Gunter Genentech, Inc. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Mike Jones Sent: Thursday, December 27, 2007 1:35 PM To: r-help at stat.math.ethz.ch Subject: [R] Conditionally incrementing a loop counter 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. 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.
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]]
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
Nordlund, Dan (DSHS/RDA)
2007-Dec-27 23:58 UTC
[R] Conditionally incrementing a loop counter
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Mike Jones > Sent: Thursday, December 27, 2007 1:35 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Conditionally incrementing a loop counter > > 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. > > > > > Mike Jones > Westat > 1650 Research Blvd. RE401 > Rockville, MD 20850 > Ph: 240.314.2312 >Mike, A question about R from SAS Mecca ??? :-) I would use Peter Dalgaard's suggestion of a 'while' type loop along with the 'next' loop control, something like this i <- 1 iter_number <- 0 while(i<=10){ iter_number <- iter_number + 1 x <- runif(1) if (x > 0.7) next cat(iter_number, i, x, "\n") i <- i+1 } Where iter_number allows you to print out loop number each time the condition is met (for instructional purposes only). When the if condition is met, the next construct skips back to the top of the while loop without executing the rest of the statements. Hope this is helpful, Dan Daniel J. Nordlund Research and Data Analysis Washington State Department of Social and Health Services Olympia, WA 98504-5204