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:
cars of the world
myvector<-c("kite","cars","box kites","model cars","i like kites that fly",
"cars of the world")
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With