gpt4 book ai didi

R:中断 for 循环

转载 作者:行者123 更新时间:2023-12-03 05:43:55 29 4
gpt4 key购买 nike

你能确认下一个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,])
}
}
}

最佳答案

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

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

所以是的,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
}
}
}

关于R:中断 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6082655/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com