Search code examples
rtextstringrgrepl

check if text vector contains a certain text in R


What is the best method to check to see if a text is in another variable in R? Below is an example:

a= 'homer simpson the third'
b= c('homer simpson','marge','bart')

grepl(a,b)
# this is what it returns
FALSE FALSE FALSE

I am wanting it to return TRUE FALSE FALSE.


Solution

  • Perhaps, use agrep with a custom value for the max.distance

    agrepl(a, b, max.distance = 0.5)
    [1]  TRUE FALSE FALSE
    

    The reason is that each element of b seems to be a substring of the 'a' pattern