Consider the following string:
"NIKE STORE COVENT GARDEN"
Suppose we are attempting to detect which is the brand that matches from the following vector:
brands <- c("ADIDAS", "NIKE", "PUMA", "COVENT", "CONVERSE")
Below is what I did with the resulting output:
library(stringr)
> brands[str_detect("NIKE STORE COVENT GARDEN", brands)]
[1] "COVENT" "NIKE"
Clearly the brand here is "NIKE", and I know that it is consistently located before the location. Is there some way I can define a rule that in the case I detect multiple brands, that I select the one that appears earlier in the string?
NOTE: In the example above we conveniently have the brand name appear in the beginning of the string. However we sometimes have the case that the string we are considering is of the form "0123 NIKE STORE COVENT GARDEN"
[1] 2 4brandsis not in the order you think it is (maybe sorted before ? this would give this output)