【问题标题】:Error for if statement with grep使用 grep 的 if 语句出错
【发布时间】:2015-05-06 17:06:16
【问题描述】:

运行以下代码:

if(grep("sales", resume$jobs$title, ignore.case = TRUE, perl = FALSE, value = FALSE,
     fixed = FALSE, useBytes = FALSE, invert = FALSE)) {p2_feature = 1}
else if(grep("\\$[0-9]+",resume$jobs$text,ignore.case = TRUE, perl = FALSE, value = FALSE,
             fixed = FALSE, useBytes = FALSE, invert = FALSE)){p2_feature = 1}
else {p2_feature = 0}

遇到错误:

if (grep("sales", resume$jobs$title, ignore.case = TRUE, perl = FALSE, : 参数长度为零

【问题讨论】:

标签: r


【解决方案1】:

你一定在想grepl,而不是grepgrepl 返回一个逻辑值向量。 grep 只返回 TRUE 值的索引(如果没有,则长度为 0)。只需将grep 替换为grepl 就可以了。

【讨论】:

  • 知道了,谢谢。我用 Length(grep) > 0
猜你喜欢
  • 2015-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-21
  • 1970-01-01
  • 2015-04-07
相关资源
最近更新 更多