I'm trying to use an "or" statement using ignore_case with str_detect. I want to convert everything that contains "ag" to "Agricultural" and everything that contains "field" to "Agricultural"
Here's an example:
(dat <-
data.frame(Class = c("ag", "Agricultural--misc", "old field")))
This works:
(dat %>%
mutate(Class_2 = case_when(
# Aggregate all Agricultural classes
str_detect(Class, fixed("Ag", ignore_case=TRUE)) ~ "Agricultural",
# Convert 'field' to Agricultural
str_detect(Class, fixed("field", ignore_case=TRUE)) ~ "Agricultural",
TRUE ~ Class)))
But I want to condense the two lines to just one line, as below:
(dat %>%
mutate(Class_2 = case_when(
# Aggregate all Agricultural and field classes to Agricultural
str_detect(Class, fixed("Ag|field", ignore_case=TRUE)) ~ "Agricultural",
TRUE ~ Class)))
fixed(.), so you cannot use the|regex (not-fixed) pattern component