2015-05-06 78 views
1

運行下面的代碼:誤差if語句使用grep

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} 

得到一個錯誤:

Error in if (grep("sales", resume$jobs$title, ignore.case = TRUE, perl = FALSE, : argument is of length zero

+0

[如何使一個偉大的R可重現的例子?](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – zx8754

回答

1

你必須思考的grepl,不grepgrepl返回一個邏輯值的向量。 grep只返回TRUE值的索引(如果沒有,則爲長度爲0的索引)。只需將grep替換爲grepl即可,您應該沒問題。

+0

得到它謝謝。我使用長度(grep)> 0 – RLearner