R for循环跳转到下一次迭代ifelse

时间:2015-08-18 15:46:50

标签: r for-loop

假设您有一个像这样的for循环

for(n in 1:5) {
  #if(n=3) # skip 3rd iteration and go to next iteration
  cat(n)
}

如果满足某个条件,如何跳到下一次迭代?

2 个答案:

答案 0 :(得分:123)

for(n in 1:5) {
  if(n==3) next # skip 3rd iteration and go to next iteration
  cat(n)
}

答案 1 :(得分:-1)

如果你想跳出循环,像这样:

for(n in 1:5) { if(n==3) break # jump out of loop, not iterating cat(n) }