RLearner
RLearner

Reputation: 11

Error for if statement with grep

Running the following code:

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}

Getting an Error:

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

Upvotes: 1

Views: 994

Answers (1)

Max Candocia
Max Candocia

Reputation: 4385

You must be thinking of grepl, not grep. grepl returns a vector of logical values. grep just returns the indices of TRUE values (which is of length 0 if there are none). Just replace grep with grepl and you should be fine.

Upvotes: 1

Related Questions