Will Phillips
Will Phillips

Reputation: 837

R Identifing text string within column of dataframe

One column of my data frame has words and phrases. I am trying to create a dummy variable for those fields within this column that have specific strings of text anywhere within.

For example:

I would want to identify all the fields with the string "kite"

I've tried a few things such as any(), which() and %in% but nothing has worked so far.

Any help greatly appreciated

Upvotes: 10

Views: 48166

Answers (1)

Luciano Selzer
Luciano Selzer

Reputation: 10016

You didn't provided any reproducible example. But your answer will be grepl.

grepl("kite", df$words)

It will return a logical vector if the word is in the row.

If you want to match multiple words use logical or | inside the string to match

grepl("kite|cars|box kites", df$words)

Upvotes: 33

Related Questions