Hi all, I need a little help with flow control in R. What I'd like to do is to advance a for loop by changing its counter. However, what seems obvious to me does not yield the proper results. An example of my problem is for (i in seq(1, some_number, some_increment)){ <some stuff> if (some_condition == T) i <- i + 2; #want to advance the loop by 2 } Whenever the counter goes to the next step, the next item in the original sequence seq(1, some_number, some_increment) replaces the counter value. Is there a way for me to do this? Best, Ken
Hi Ken, The help page for ?"for" says that: The index seq in a for loop is evaluated at the start of the loop; changing it subsequently does not affect the loop. The variable var has the same type as seq, and is read-only: assigning to it does not alter seq. So you cannot do what you want to do with a for loop. But you could do what you want with a while loop: i <- 0 while(i < 20) { i <- i + 1 cat(i, "\n") if(i %% 5 == 0) i <- i + 2 } -Christos> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Lo, Ken > Sent: Wednesday, March 04, 2009 3:53 PM > To: r-help at r-project.org > Subject: [R] FW: flow control > > Hi all, > > I need a little help with flow control in R. What I'd like > to do is to advance a for loop by changing its counter. > However, what seems obvious to me does not yield the proper > results. An example of my problem is > > > for (i in seq(1, some_number, some_increment)){ > <some stuff> > if (some_condition == T) i <- i + 2; #want to advance > the loop by 2 } > > Whenever the counter goes to the next step, the next item in > the original sequence seq(1, some_number, some_increment) > replaces the counter value. Is there a way for me to do this? > > Best, > > Ken > > > > ______________________________________________ > 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. > >
The help page for ?"for" says that: The index seq in a for loop is evaluated at the start of the loop; changing it subsequently does not affect the loop. The variable var has the same type as seq, and is read-only: assigning to it does not alter seq. The help file is not right when seq is a list() or other recursive type. In that case var has the type of seq[[i]] where i is the current iteration count. (I think this is true in general, since [ and [[ act the same for nonrecursive types when the indices are such that a scalar would be returned. However that explanation is unnecessarily complicated in the nonrecursive case.) Also, the variable var is not really read-only. You can alter it but it gets reset to the next value in seq at the start of each iteration. You cannot affect the meaning of 'next' to force it to, e.g, omit or repeat iterations. Bill Dunlap TIBCO Software Inc - Spotfire Division wdunlap tibco.com
Reasonably Related Threads
- chan_capi problem - hangup???
- ActiveRecord: When / where to validate data? Tricky question
- Is active record 3.1.1 supposed to be threadsafe?
- Refresh a partial onClick using ajax call in rails 3.x
- [LLVMdev] Separating loop nests based on profile information?