partial string matching - R

Posted by DonDyck on Stack Overflow See other posts from Stack Overflow or by DonDyck
Published on 2014-05-28T01:30:55Z Indexed on 2014/05/28 3:25 UTC
Read the original article Hit count: 113

Filed under:
|
|

I need to write a query in R to match partial string in column names. I am looking for something similar to LIKE operator in SQL. For e.g, if I know beginning, middle or end part of the string I would write the query in format:

LIKE 'beginning%middle%' 

in SQL and it would return matching strings. In pmatch or grep it seems I can only specify 'beginning' , 'end' and not the order. Is there any similar function in R that I am looking for?

For example, say I am looking in the vector:

y<- c("I am looking for a dog",
      "looking for a new dog", "a dog", "I am just looking")

Lets say I want to write a query which picks "looking for a new dog" and I know start of the string is "looking" and end of string is "dog". If I do a grep("dog",y) it will return 1,2,3. Is there any way I can specify beginning and end in grep?

© Stack Overflow or respective owner

Related posts about regex

Related posts about r