Hi,
I have a df withmodel names starting with a number 2, 3 or 6.
source <- data.frame(
stringsAsFactors = FALSE,
Model3 = c("1.2","1.2 C","2","2-1.3",
"2-1.4","2-1.5","2-GT-","2-WHI","2 (10","2 (11",
"2 .","2 ..","2 1","2 1.","2 1..","2 1.2","2 1.3",
"2 1.4","2 1.5","3 2-3","2 2 (","3 2 1","2 2 3","6 2 5",
"2 2 A","2 3DR","2 4DR","2 5","2 5-D","2 5 D",
"2 55","2 5DO","2 5DR","2 75P","2 76","2 90","2 90P",
"2.2TM","2.3","2DR C","2TS","3","3-1.6","3-2.0",
"3 (12","6","6-1.8","6-2..","6-2.0","6 (14","6 (15"),
Score = c(6,1,30252,19,1,18,3,2,1,
10,4,1,1,21,1,128,23938,1660,39640,1,6,972,
41,1034,5,2644,2,1,5,175,2,28,54054,227,6,1,
1464,1225,1,224,2,25922,26,42,183,30808,4,10,
14,9,41)
)
source
I use this to recode Model3 into ModelCat where model names starting from 2 should be coded as '2', starting from 3 as '3' and starting from 6 as '6'. I use this code:
library(dplyr)
result <- source %>%
mutate(Model3=as.character(Model3)) %>%
mutate(ModelCat = case_when(
grepl(x = Model3, pattern = '^20|^21|^23|^25', ignore.case = TRUE) ~ 'Other',
grepl(x = Model3, pattern = '^2.1|^2.2|^2.3|^2.4|^2.5|^2.6', ignore.case = TRUE) ~ 'Other',
grepl(x = Model3, pattern = '^2|^2\\s|^2-', ignore.case = TRUE) ~ '2',
grepl(x = Model3, pattern = '2\\s1.2|2\\s1.3|2\\s1.4|2\\s1.5|2\\s1.6|2\\s2.7|2\\s2.8|2\\s3dr|2\\s5dr', ignore.case = TRUE) ~ '2',
grepl(x = Model3, pattern = '^30|^32|^33|^35', ignore.case = TRUE) ~ 'Other',
grepl(x = Model3, pattern = '^3|^3\\s|^3-', ignore.case = TRUE) ~ '3',
grepl(x = Model3, pattern = '^62|^65', ignore.case = TRUE) ~ 'Other',
grepl(x = Model3, pattern = '^6|^6\\s|^6-', ignore.case = TRUE) ~ '6',
TRUE ~ "Other"
))
result
I have tried multiple options (that is why the code is a bit messy) but, for some reason, 3s and 6s are properly recoded whereas I have problems with recoding 2s. I really do not know why. Can anyone help?