Reputation: 165
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"
Upvotes: 2
Views: 124
Reputation: 1253
You can consider using str_locate instead of str_detect. What about :
brands[which.min(str_locate("NIKE STORE COVENT GARDEN", brands)[,1])]
Upvotes: 2