[R] How to subtract the counter i in for loop?

Berend Hasselman bhh at xs4all.nl
Fri Nov 30 06:24:38 CET 2012


On 30-11-2012, at 05:47, C W wrote:

> David,
> Your results is,
> 
>> samples
> 
> [1] 111  NA 111  NA 111  NA 111  NA 111  NA
> It still has NA's in it. I want it look like this,
> 
>> samples
> 
> [1] 111  111  111  111 111

You don't need the for loop at all.

samples <-  x[x<200]

or

samples <-  x[which(x<200)]

Your for loop should look something like this

k <- 1
for(i in 1:10){
  if(x[i]<200){
       samples[k] <- x[i]
       k <- k+1
   }
}
na.omit(samples)

Berend




More information about the R-help mailing list