0

I'm using the following code to find if the word "assist" is used in a string variable. string<- c("assist")

`assist <-
(1:nrow(df) %in% c(sapply(string, grep, df$textvariable, fixed = TRUE)))+0`

`sum(assist)`

If I also wanted to check if synonyms such as "help" and "support" are used in the string, how can I update the code? So if either of these synonyms are used, I want to code it as 1. If neither of these words are used, I want to code it as 0. It doesn't matter if all of the words appear in the string or how many times they are used.

I tried changing it to

string<- c("assist", "help", "support")

But it looks like it is searching for strings in which all of these words are used?

I'd appreciate your help!

Thank you

Progman
  • 16,827
  • 6
  • 33
  • 48
  • Likely duplicates: https://stackoverflow.com/questions/35962426/multiple-strings-with-str-detect-r or https://stackoverflow.com/questions/7597559/grep-using-a-character-vector-with-multiple-patterns – Ian Campbell Apr 20 '21 at 18:37
  • Thank you Ian. I checked the posts you referenced and they are discussing different cases. – user15708467 Apr 20 '21 at 20:23

0 Answers0