Mike, I'm still not sure what you want samples to look like, but here are a couple possibilities, using a different x.
library(zoo) x <- c(50, 250, 100, 300, 300, 20, 40, 800) samples1 <- ifelse(x<200, x, NA) samples2 <- na.locf(samples1) samples3 <- samples1[!is.na(samples1)] samples1 samples2 samples3 > samples1 [1] 50 NA 100 NA NA 20 40 NA > samples2 [1] 50 50 100 100 100 20 40 40 > samples3 [1] 50 100 20 40 Jean C W <tmrs...@gmail.com> wrote on 11/29/2012 05:16:49 PM: > > Hi Jean, > I am going through vector x, if it is less than 200, fill it into samples. > > I am having trouble with indexing x. > > For example, > samples <-rep(NA, 10) > > at i=2 > x[2], 225<200 is not true; and samples[2] remains NA. > > The loop continues to i=3, leaving x[2]=NA. I don't want that to > happen, I want to fill up x[2] with value, in our case it's 111. > > Mike > > On Thu, Nov 29, 2012 at 5:39 PM, Jean V Adams <jvad...@usgs.gov> wrote: > 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 <tmrs...@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 the i > > in "else" is not getting subtracted. How should I get it to work? > > > > Thanks, > > Mike [[alternative HTML version deleted]] ______________________________________________ R-help@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.