-1

Very new to R and hoping for help.

I have a list of 1000 product names, and I have a list of 80 key words or phrases. I need to determine how many of the 1000 product names contain one or more of those key words or phrases.

Example: if one of the 1000+ product names was "honey bunches of oats" and one of the 80+ keywords is "honey", I need it to show up as TRUE in a new column next to "honey bunches of oats"

Uploaded both lists as csv files. I made a vector for each list, and tried to use the following:

str_detect(products, regex(".keywords.", ignore_case = TRUE))

This came back with all false results. I also tried to use grepl(keywords, products) which returned zero results as well.

I am confident there should be instances where the keywords are contained within these strings. Is it looking for exact matches? I need it to show partial matches.

3
  • Please share a small reproducible example of your data.
    – deschen
    Feb 8 at 6:00
  • @deschen 2 examples of products: "36262722 - ABC CAJUN SALT 7373 H" and "737171 - CARIBBEAN SALT+25#ABCD" 2 examples of keywords "CAJUN" and "CARIBBEAN SALT"
    – TWilliams
    Feb 8 at 6:07
  • Not as a comment, but as real code objects in your question. See: stackoverflow.com/help/minimal-reproducible-example
    – deschen
    Feb 8 at 6:09

1 Answer 1

0

Try:

products <- c('apple hello', 'banana', 'peach', 'a')
.keywords. <- c('apple', 'each')

library(stringr)
str_detect(products, paste0(.keywords., collapse = '|'))

# [1]  TRUE FALSE  TRUE FALSE
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.