user36729
user36729

Reputation: 575

Check to See if a Word is in a Sentence

I have a column in my data frame containing sentences like below:

c("Animation", "Comedy", "Family")

How can I check to see if there is any special word such as "Animation" in the sentence?

Upvotes: 0

Views: 72

Answers (1)

onlyphantom
onlyphantom

Reputation: 9613

Create the data for us to work with:

somedat <- c("Animation", "Comedy", "Family", "Animation2")

If you only want to know if the column contain that exact word, you can use:

"Animation" %in% somedat
# returns TRUE

If you want to get the index of the word (or row number) if it's in the column:

grep(pattern="Animation", somedat)
# returns 1 4

If you want to get the word returned to you:

grep(pattern="Animation", somedat, value=T)
# returns [1] "Animation"  "Animation2"

Upvotes: 1

Related Questions