我在R中有一个for循环:
for i in seq(1 , nrow(df)){
try(function(i),silent=T)
}
我想对此进行修改,以便在显示错误消息时,该函数再重复一次for循环。如果再次失败-
message(paste("function failed twice for", i))
`
try()是否适合此目的?
最佳答案
我不知道任何R,但我猜测您可以执行以下操作:
errorCount = 0
for i in seq(1 , nrow(df)){
tryCatch(function(i),silent=T,
error = {errorCount +=1,
tryCatch(function(i), silent=T,
error = {errorCount +=1
print("2 times error")
tryCatch(function(i), silent=T,
error = {errorCount +=1
tryCatch(function(i), silent=T,
error = {print("3 times error: Did not repeat")
break})
} )
})
)
}
对不起,我的代码很糟糕-我按照您指定的那样进行了3次,但是您必须修复语法。建议其他答案。
关于r - 错误打破for循环: conditional try statement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59381916/