Reputation: 11793
I have a dataframe like below:
dfm = data.frame (names = c('email', 'Facebook', 'walmart', 'target', 'instagram', 'costco'))
I need to create a new column source
. When names are facebook, instagram or email, source is media. When names are costco,walmart or target, the source is store.
I use case_when
and str_detect
. I need to the str_detect
to be case insensitive. So below is my code.
dfm %>%
mutate(source = case_when( str_detect(names, fixed('email|facebook|instagram', ignore_case = T))~'media',
str_detect(names, 'walmart|costco|target')~ 'store'))
I got:
names source
email NA
Facebook NA
walmart store
target store
instagram NA
costco store
I do not understand why it did not work. Does anyone know why?
I tried the code below, it returns TRUE
str_detect('Facebook', fixed('facebook', ignore_case = T))
Upvotes: 1
Views: 2759
Reputation: 199
Also try regex instead of fixed:
dfm %>%
mutate(source = case_when( str_detect(names, regex('email|facebook|instagram', ignore_case = T))~'media',
str_detect(names, 'walmart|costco|target')~ 'store'))
Upvotes: 4
Reputation: 388862
When you are using fixed
it will not recognize |
as regex. If you want to do exact match use word boundaries i.e \\b
.
library(dplyr)
library(stringr)
dfm %>%
mutate(source = case_when(str_detect(names,
regex('\\bemail\\b|\\bfacebook\\b|\\binstagram\\b',
ignore_case = TRUE))~'media',
str_detect(names, 'walmart|costco|target')~ 'store'))
# names source
#1 email media
#2 Facebook media
#3 walmart store
#4 target store
#5 instagram media
#6 costco store
Upvotes: 4