R:中断 for 循环

发布于 2024-11-09 01:40:23 字数 358 浏览 0 评论 0原文

你能确认下一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北笙凉宸 2024-11-16 01:40:23

好吧,您的代码不可重现,所以我们永远无法确定,但这就是 help('break') 所说的:

break 中断于 for、while 或
重复循环;控制权转移到
外部的第一个语句
最内层循环。

所以是的,break 只会中断当前循环。您还可以通过以下方式查看它的实际效果:

for (i in 1:10)
{
    for (j in 1:10)
    {
        for (k in 1:10)
        {
            cat(i," ",j," ",k,"\n")
            if (k ==5) break
        }   
    }
}

Well, your code is not reproducible so we will never know for sure, but this is what help('break')says:

break breaks out of a for, while or
repeat loop; control is transferred to
the first statement outside the
inner-most loop.

So yes, break only breaks the current loop. You can also see it in action with e.g.:

for (i in 1:10)
{
    for (j in 1:10)
    {
        for (k in 1:10)
        {
            cat(i," ",j," ",k,"\n")
            if (k ==5) break
        }   
    }
}
月依秋水 2024-11-16 01:40:23

你的break语句应该脱离for (in in 1:n)

就我个人而言,我总是对break语句保持警惕,并通过打印到控制台来仔细检查它,以仔细检查我实际上是否打破了正确的循环。因此,在测试之前添加以下语句,这将让您知道是否在到达末尾之前中断。但是,我不知道您如何处理变量 n 所以我不知道这对您是否有帮助。创建一个 n 一些测试值,您可以事先知道在达到 n 之前它是否应该爆发。

for (in in 1:n)
{
    if (in == n)         #add this statement
    {
        "sorry but the loop did not break"
    }

    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,])
    }
}

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 a n some test value where you know before hand if it is supposed to break out or not before reaching n.

for (in in 1:n)
{
    if (in == n)         #add this statement
    {
        "sorry but the loop did not break"
    }

    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,])
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文