Hi list, I am writing a for loop that looks like this: samples<-rep(NA,10) x <- rep(c(111, 225), 5) for(i in 1:10){ If(x[i]<200){ samples[i] <- x[i] }else{ i=i-1 } } The problem is that the returning vector still contains NA, I think the i in "else" is not getting subtracted. How should I get it to work? Thanks, Mike [[alternative HTML version deleted]]
Mike, Based on this example, what do you want samples to look like? It's not clear to me what you're trying to do with i-1 Jean C W <tmrsg11@gmail.com> wrote on 11/29/2012 03:55:12 PM:> > Hi list, > I am writing a for loop that looks like this: > samples<-rep(NA,10) > x <- rep(c(111, 225), 5) > for(i in 1:10){ > If(x[i]<200){ > samples[i] <- x[i] > }else{ > i=i-1 > } > } > > The problem is that the returning vector still contains NA, I think thei> in "else" is not getting subtracted. How should I get it to work? > > Thanks, > Mike[[alternative HTML version deleted]]
Use a while loop instead of a for loop. I don't think what you have coded makes any sense, but fighting the for loop over control of the indexing variable is a recipe for failure. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. C W <tmrsg11 at gmail.com> wrote:>Hi list, >I am writing a for loop that looks like this: >samples<-rep(NA,10) >x <- rep(c(111, 225), 5) >for(i in 1:10){ > If(x[i]<200){ > samples[i] <- x[i] > }else{ > i=i-1 > } >} > >The problem is that the returning vector still contains NA, I think >the i >in "else" is not getting subtracted. How should I get it to work? > >Thanks, >Mike > > [[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.
On Nov 29, 2012, at 1:55 PM, C W wrote:> Hi list, > I am writing a for loop that looks like this: > samples<-rep(NA,10) > x <- rep(c(111, 225), 5) > for(i in 1:10){ > If(x[i]<200){ > samples[i] <- x[i] > }else{ > i=i-1If you expected the else clause to assign something to the samples vector, you are mistaken.> } > } > > The problem is that the returning vector still contains NA, I think the i > in "else" is not getting subtracted. How should I get it to work?You could start by telling us what you wanted to happen. You can change the index of a for loop inside the body, but it will not "back up the process" since at the end of the loop the next "i" will not depend on what you changed it to inside the loop. -- David Winsemius, MD Alameda, CA, USA