changing loop index within loop
Asked Answered
B

5

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

Bakken answered 6/5, 2011 at 14:57 Comment(4)
Sami - can you post some sample data?Naidanaiditch
Please give us some example code...Perforated
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.Lynea
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.Bakken
R
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
  }
}
Racing answered 6/5, 2011 at 15:8 Comment(2)
You can change the index variable within a for-loop but it won't "stick".Lynea
@DWin: Agreed. I meant "change" in the sense the OP wanted--skipping values.Racing
O
9

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.

Ouzo answered 6/5, 2011 at 15:41 Comment(0)
A
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])
}
Accad answered 6/5, 2011 at 15:7 Comment(0)
L
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
Lynea answered 6/5, 2011 at 15:14 Comment(1)
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.Racing
M
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.

Mara answered 23/7 at 0:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.