Claire Wilson
Claire Wilson

Reputation: 7

Why isn't str_count working with multiple strings?

I have a string with text like this:

Text <- c("How are you","What is your name","Hi my name is","You ate your cake")

And I want an output that counts the number of times the word "you" or "your" appears

Text                   NumYou
"How are you"          1
"What is your name"    1
"Hi my name is"        0
"You ate your cake"    2

I tried using the str_count function but it was missing occurrences of "you" and "your"

NumYou = str_count(text,c("you","your"))

Why isn't str_count working correctly?

Upvotes: 0

Views: 486

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388982

Pass the pattern as one string.

stringr::str_count(tolower(Text),'you|your')
#[1] 1 1 0 2

Upvotes: 2

Related Questions