mardi 15 mai 2018

Using if and next to skip an iteration in for-loop

Sample data

x <- c("a","b","c")
y <- c(1,2,3,4)

for(i in seq_along(x)){

   x.sub <- x[i]

  if(x.sub == "b") next 
  print(x.sub)

 # rest of the long code
}

If I want to skip the iteration when x = b and y = 2

for(i in seq_along(x)){
  for(j in seq_along(y)){

    x.sub <- x[i]
    y.sub <- y[j]

    if(x.sub == "b" & y.sub == 2) next # this bit is wrong.
    print(x.sub);print(y.sub)

  # rest of the long code
 }
}

How do I fix it? i.e. the output should skip from x == b & y == 1 to x == b & y == 3

Aucun commentaire:

Enregistrer un commentaire