You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言for循环中遇logical(0)时跳过当前迭代的实现方法

解决R语言循环中跳过logical(0)的问题

你的代码失效是因为判断逻辑错误:logical(0)本身属于logical类型,is.logical(kl)会返回TRUE,导致所有正常返回TRUE/FALSE的迭代也被跳过,完全搞反了需求。

要实现当kl是logical(0)时跳过当前迭代,需要检查kl的长度是否为0,正确代码如下:

for (...) {
  kl <- stri_detect_fixed(nn, "new")
  # 检测到logical(0)时跳过
  if (length(kl) == 0) {
    next
  }
  # 这里写后续需要执行的代码
}

如果需要更严谨(避免非logical类型的空值干扰),可以结合类型判断:

if (is.logical(kl) && length(kl) == 0) {
  next
}

内容的提问来源于stack exchange,提问作者user24236624

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 05:42:02