Hi,
I have a list of car models:
source <- data.frame(
stringsAsFactors = FALSE,
URN = c("GB0421600680972799",
"GB0414160870904854","GB0411001930874092","GB0421589830971661",
"GB0411001960875261","GB0409850670845457",
"GB0418417380942180","GB0414194530911963","GB0417357570936378",
"GB0217347470934167","GB0415237340919038",
"GB0419459860949630","GB0420557080966254","GB0408770490827808",
"GB0216303060926766","GB0418417380941723","GB0210937840862352"),
FamilyName = c("2008 (A94)","e2008",
"2008 (A94)","2008 V2 (P24)","2008 V2 (P24)","e208",
"208 (A9)","208 (A9)","208 V2 (P21)","208 V2 (P21)",
"BERLINGO (K9 EUROPE)","Corsa 15",
"BERLINGO V2 (M59)","Corsa 07","New 2008",
"BERLINGO V3 (B9)","CORSA (P2JO)")
)
where I am trying to recode names:
library(dplyr)
result <- source %>%
mutate(FamilyNameCat = case_when(
grepl(x = FamilyName, pattern = 'Nuovo\\s2008|NEUER\\s2008|New\\s2008|Nuevo\\s2008|Nuova\\s2008|NV\\s2008|2008\\sNeu|2008\\sNlle', ignore.case = TRUE) ~ 'Peugeot 2008 New',
grepl(x = FamilyName, pattern = 'e2008', ignore.case = TRUE) ~ 'Other',
grepl(x = FamilyName, pattern = '2008\\sV2', ignore.case = TRUE) ~ 'Peugeot 2008 V2',
grepl(x = FamilyName, pattern = '2008', ignore.case = TRUE) ~ 'Peugeot 2008',
grepl(x = FamilyName, pattern = 'e208', ignore.case = TRUE) ~ 'Other',
grepl(x = FamilyName, pattern = '208\\sV2', ignore.case = TRUE) ~ 'Peugeot 208 V2',
grepl(x = FamilyName, pattern = '208', ignore.case = TRUE) ~ 'Peugeot 208',
grepl(x = FamilyName, pattern = 'Berlingo\\sV2', ignore.case = TRUE) ~ 'Other',
grepl(x = FamilyName, pattern = 'Berlingo\\sV3', ignore.case = TRUE) ~ 'Other',
grepl(x = FamilyName, pattern = 'Berlingo', ignore.case = TRUE) ~ 'Citroen Berlingo K9',
grepl(x = FamilyName, pattern = 'Corsa\\s15', ignore.case = TRUE) ~ 'Vauxhall Corsa 15',
grepl(x = FamilyName, pattern = 'Corsa&P2', ignore.case = TRUE) ~ 'Vauxhall Corsa P2',
TRUE ~ "Other"
))
I don't know why the same pattern works for 2008 but does not for 208.
Also, I don't know how to fix Berlingo recoding and how to code Corsa (P2JO).
Can you help?