11

I am relatively new to R. I am iterating over a vector in R by using for() loop. However, based on a certain condition, I need to skip some values in the vector. The first thought that comes to mind is to change the loop index within the loop. I have tried that but somehow its not changing it. There must be some what to achieve this in R.

Thanks in advance. Sami

4
  • Sami - can you post some sample data? Commented May 6, 2011 at 15:07
  • Please give us some example code... Commented May 6, 2011 at 15:08
  • It probably is changing the index within the loop but the altered value is not changing the indexing for the next pass. The indexing at the control level is not affected by your local alteration of the index variable. Commented May 6, 2011 at 15:09
  • Okay from the examples, I think it is clear that you can't change the loop index within the loop. I guess I would have to use "while" loop. But thanks for the answers anyway. Commented May 8, 2011 at 4:43

5 Answers 5

17

You can change the loop index within a for loop, but it will not affect the execution of the loop; see the Details section of ?"for":

 The ‘seq’ in a ‘for’ loop is evaluated at the start of the loop;
 changing it subsequently does not affect the loop. If ‘seq’ has
 length zero the body of the loop is skipped. Otherwise the
 variable ‘var’ is assigned in turn the value of each element of
 ‘seq’. You can assign to ‘var’ within the body of the loop, but
 this will not affect the next iteration. When the loop terminates,
 ‘var’ remains as a variable containing its latest value.

Use a while loop instead and index it manually:

i <- 1
while(i < 100) {
  # do stuff
  if(condition) {
    i <- i+3
  } else {
    i <- i+1
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

You can change the index variable within a for-loop but it won't "stick".
@DWin: Agreed. I meant "change" in the sense the OP wanted--skipping values.
10

Look at

?"next"

The next command will skip the rest of the current iteration of the loop and begin the next one. That may accomplish what you want.

Comments

2

Without an example it is hard to see what you want to do, but you can always use an if-statement inside a for-loop:

foo <- 1:10*5
for (i in seq(length(foo)))
{
 if (foo[i] != 15) print(foo[i])
}

Comments

2

In R, local alterations in the index variable are "corrected" with the next pass:

 for (i in 1:10){
    if ( i==5 ) {i<-10000; print(i)} else{print(i)}
                 }
#-----
[1] 1
[1] 2
[1] 3
[1] 4
[1] 10000
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10

Since you have some criterion for skipping you should apply the criterion to the loop vector inside the for-parentheses. E.g:

 for( i in (1:10)[-c(3,4,6,8,9)] ) {
          print(i)}
#----
[1] 1
[1] 2
[1] 5
[1] 7
[1] 10

1 Comment

Skipping via the seq argument to the for loop assumes you a-priori know which elements to skip. If that's the case, you may be able to use a vectorized solution and skip the loop completely.
0

Well the for loop and the while can be used different, Here is how you can use them for different results

The while loop

i <- 1
ten <- 10
while(i < ten) {
  if(i>5) {
  ten <- ten-1
} 
print(i)
i<-i+1
}

Result:

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
> ten
[1] 8

The for loop

tin <- 10
for (l in 1:tin) {
  if(l>5) {
  tin <- tin-1
} 
print(l)
}

Result:

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
> tin
[1] 5

Conclusion: The for loop index when initiated, can't be changed but the while loop can be changed within the loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.