Skip to content

How to subtract the counter i in for loop?

12 messages · C W, Jeff Newmiller, Jean V Adams +2 more

#
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:

            
#
On Nov 29, 2012, at 1:55 PM, C W wrote:

            
If you expected the else clause to assign something to the samples vector, you are mistaken.
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.
#
On 30-11-2012, at 05:47, C W wrote:

            
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
#
On 30-11-2012, at 07:18, C W wrote:

            
It's not clear what you exactly want.
A matrix with 15 rows and some columns?
How do you want to remove the NA's in each column?
Then why don't you leave out the NA in the sample?

Berend