R:中断 for 循环
你能确认下一个break是否取消了内部for循环吗?
for (out in 1:n_old) {
id_velho <- old_table_df$id[out]
for (in in 1:n) {
id_novo <- new_table_df$ID[in]
if(id_velho==id_novo) {
break
} else
if(in == n) {
sold_df <- rbind(sold_df,old_table_df[out,])
}
}
}
Can you confirm if the next break cancels the inner for loop?
for (out in 1:n_old) {
id_velho <- old_table_df$id[out]
for (in in 1:n) {
id_novo <- new_table_df$ID[in]
if(id_velho==id_novo) {
break
} else
if(in == n) {
sold_df <- rbind(sold_df,old_table_df[out,])
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,您的代码不可重现,所以我们永远无法确定,但这就是
help('break')
所说的:所以是的,
break
只会中断当前循环。您还可以通过以下方式查看它的实际效果:Well, your code is not reproducible so we will never know for sure, but this is what
help('break')
says:So yes,
break
only breaks the current loop. You can also see it in action with e.g.:你的break语句应该脱离
for (in in 1:n)
。就我个人而言,我总是对break语句保持警惕,并通过打印到控制台来仔细检查它,以仔细检查我实际上是否打破了正确的循环。因此,在测试之前添加以下语句,这将让您知道是否在到达末尾之前中断。但是,我不知道您如何处理变量
n
所以我不知道这对您是否有帮助。创建一个n
一些测试值,您可以事先知道在达到n
之前它是否应该爆发。your break statement should break out of the
for (in in 1:n)
.Personally I am always wary with break statements and double check it by printing to the console to double check that I am in fact breaking out of the right loop. So before you test add the following statement, which will let you know if you break before it reaches the end. However, I have no idea how you are handling the variable
n
so I don't know if it would be helpful to you. Make an
some test value where you know before hand if it is supposed to break out or not before reachingn
.