How to return TRUE/FALSE based on text match with Regular Expression


I am trying to create a new column that reads TRUE if (at least) one word from a list of keywords is detected in a string in my base column (and FALSE if none of the keywords exist in the string).

I tried something simple (using the str_detect function) like this but it does not work…

str_detect function takes a regular expression so you can write something like this to make this work.

mutate(Description_new = str_detect(Description, “Assist|Retake”))

Let me know if this works for you. And if you are not big fan of regular expression like me, you could do something like this instead.

mutate(Description_new = Description %in% c(“Assist”,“Retake”))

Here’s an regular expression in R, only if you’re interested in.

1 Like

1st option within str_detect works perfectly (I should have got that :blush:) - Many thanks…

2nd option using %in% does not seem to work for my case (seems to only work for complete matches, i.e., matching full string to full string…).

Oops, sorry, my bad. For some reason, you were trying to do the exact match. You are right, in that case the 1st option is the one for you! Thanks for confirming!