假设您有一个像这样的for循环
for(n in 1:5) {
#if(n=3) # skip 3rd iteration and go to next iteration
cat(n)
}
如果满足特定条件,如何跳到下一个迭代?
Answers:
for(n in 1:5) {
if(n==3) next # skip 3rd iteration and go to next iteration
cat(n)
}
?Control
类似功能
for(n in 1:5) { if(n==3) print ('3rd iteration' ) next # skip 3rd iteration and go to next iteration cat(n) }
我想打印的事情 ,那我将要跳过第3次迭代,那会导致什么情况,那么在某些情况下,我们需要记录跳过的内容以使事情易于处理。对此有何想法?
if
声明添加额外的括号,例如for(n in 1:5) { if(n==3) { print ('3rd iteration' ) ; next } # skip 3rd iteration and go to next iteration cat(n) }
for(n in 1:5){if(n!=3){cat(n)}}