Home > Blockchain >  Using the grep function in R despite different upper and lower cases
Using the grep function in R despite different upper and lower cases

Time:11-02

I'm currently trying to check whether the text elements:

brands <- c("Nike", "Adidas, "D&G")

are to be found in the vector:

text <- c("I love Nike", "I love Adidas")

For this I use the code:

brands_subset <- unique (grep(paste(text,collapse="|"), brands, value=TRUE))

However, this only works if the upper and lower case of the brands and text elements match. Is there any way to find a match between the brands and text elements even if I had text elements like:

text <- c("I love nike", "I love Adidas").

CodePudding user response:

  1. You need to paste/collapse the patterns, not the text, so paste(brands,..).
  2. grep(..., ignore.case=TRUE).
brands <- c("nike", "adidas", "d&g")  # lower-case here
text <- c("I love Nike", "I love Adidas")
unique (grep(paste(brands,collapse="|"), text, value=TRUE, ignore.case=TRUE))
# [1] "I love Nike"   "I love Adidas"
  • Related