简体   繁体   中英

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:

  • kite
  • cars
  • box kites
  • model cars
  • i like kites that fly
  • 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM