I am trying to implement some expert rules based on the presence or absence of words in a sentence. I have given a reproducible example below. In this, every time I come across the words lunch and bag in the same sentence, the outcome would be 1. If lunch and pack are in the same sentence, then the outcome would be 2. If only lunch is present, the outcome would be 3. There is no guarantee that these two words (lunch/bag or lunch/pack) will be next to each other in the sentence. I tried to implement this using regexpr but the last rule (lunch -> 3) supersedes the other two rules. This works fine only if I have lunch only as the first rule following by the other two.
Is there a way to make sure outcome will be 3 if only lunch is present? (I have hundreds of rules; Hence, finding out the correct order manually is not possible( keyWord <- c("lunch bag","lunch pack","lunch") outcome <- c(1,2,3) expertRules<- data.frame(keyWord = keyWord, outcome = outcome) testWords <- c("lunch pack","lunch","lunch","lunch bag","lunch pack") predictedOutcome <- c(NA,NA,NA,NA,NA) testDf <- data.frame(testWords = testWords, predictedOutcome = predictedOutcome) for(i in 1:nrow(expertRules)) { testDf$predictedOutcome <- ifelse((regexpr(expertRules[i,1],testDf$testWords)>0), expertRules[i,2], testDf$predictedOutcome) } > testDf testWords predictedOutcome 1 lunch pack 3 2 lunch 3 3 lunch 3 4 lunch bag 3 5 lunch pack 3 Thank you. Ravi -- View this message in context: http://r.789695.n4.nabble.com/Checking-for-combination-of-words-in-a-sentence-tp3570104p3570104.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.