Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading