5

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")

Let's 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?

4
  • Can you show some test cases where grep failed you? Commented May 28, 2014 at 1:36
  • For example lets say I want to look into 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,4. Is there any way I can specify beginning and end in grep? Thanks Commented May 28, 2014 at 1:46
  • I would edit your original question to include that example. That is important information. Commented May 28, 2014 at 1:51
  • grep("dog", y) should return 1 2 3 from that vector. Commented May 28, 2014 at 1:53

2 Answers 2

7

The grep function supports regular expressions and with regular expressions, you can match almost anything

y<- c("I am looking for a dog", "looking for a new dog", "a dog", "I am just looking")
grep("looking.*dog",y, value=T)
# [1] "I am looking for a dog" "looking for a new dog" 

Here this pattern looks for looking then "maybe something" then dog. So that should do what you want.

Sign up to request clarification or add additional context in comments.

Comments

4

In regular expressions, ^ specifies the beginning of the string, $ specifies the end, thus:

y<- c("I am looking for a dog", "looking for a new dog", "a dog", "I am just looking")
grep("^looking.*dog$", y)
[1] 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.