Diego
Diego

Reputation: 36166

finding exact string on data.frame

I'm trying to look for a string on a data frame with this code:

grep('of', df$term, fixed=TRUE )

but it returns "kind of" for example.

How can I get it to look only for the "of" word? (it can be another code, doesn't have to be grep)

Thanks

Upvotes: 0

Views: 804

Answers (2)

Tom Liptrot
Tom Liptrot

Reputation: 2283

It is not completely clear what you are after from the question. You want to match "of" but not "kind of"?

How about just using ==? This returns a match only when the string is exactly equal to "of" - it should be more efficient than a regular expression based approach.

text <- c("of", "kind of", "often", "toff")
text == 'of'
[1]  TRUE FALSE FALSE FALSE

Upvotes: 1

lawyeR
lawyeR

Reputation: 7664

Are you looking for something like this, using the stringr package? There are certain to be other text possibilities, of course.

text <- c("of", "lots of", "often", "toff")

find in the text string where the pattern is at the start, has "of" and ends

str_detect(text, "^of$")
[1]  TRUE FALSE FALSE FALSE

Upvotes: 1

Related Questions