Hi, I have this simple data file where I try to create ModelCat variable based on string values in ModelLong. Strings without brackets and blanks are coded properly whereas two expressions are not. Do you know what I am doing wrong?
library(dplyr)
Sales.data.t <- data.frame(stringsAsFactors = FALSE,
ModelLong = c(NA, "bbbb", "aa(2014~)", "aa(2014 ~)", "bb")
)
Sales.data.t
# Creating new variable showing main Models
Sales.mod <- Sales.data.t %>%
mutate(ModelCat = case_when(
grepl(x = ModelLong, pattern = 'aa(2014~)|aa(2014 ~)', ignore.case = TRUE) ~ 'aa (2014 ~)',
grepl(x = ModelLong, pattern = 'bb|bbbb', ignore.case = TRUE) ~ 'bb',
TRUE ~ "Other"
))
Sales.mod
I cannot find any restrictions in R documentation...