Detecting location of substring within string in R -


consider following string:

"nike store covent garden" 

suppose attempting detect brand matches following vector:

brands <- c("adidas", "nike", "puma", "covent", "converse") 

below did resulting output:

library(stringr) > brands[str_detect("nike store covent garden", brands)] [1] "covent"    "nike" 

clearly brand here "nike", , know consistently located before location. there way can define rule in case detect multiple brands, select 1 appears earlier in string?

note: in example above conveniently have brand name appear in beginning of string. have case string considering of form "0123 nike store covent garden"

you can consider using str_locate instead of str_detect. :

brands[which.min(str_locate("nike store covent garden", brands)[,1])] 

Comments

Popular posts from this blog

How to show in django cms breadcrumbs full path? -

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -