full <- full %>%
mutate(Title = case_when(
Title %in% c('Mlle', 'Ms') ~ 'Miss',
Title == 'Mme' ~ 'Mrs',
Title %in% rare_title ~ 'Rare Title',
TRUE ~ Title
))
Code above gives an error: Error in eval(substitute(expr), envir, enclos) : object 'Title' not found
However, below code works. Is the data-frame name required inside case_when (makes the code more verbose).
full <- full %>%
mutate(Title = case_when(
full$Title %in% c('Mlle', 'Ms') ~ 'Miss',
full$Title == 'Mme' ~ 'Mrs',
full$Title %in% rare_title ~ 'Rare Title',
TRUE ~ full$Title
))
We can use .$
instead of calling full$
full <- full %>%
mutate(Title = case_when(
.$Title %in% c('Mlle', 'Ms') ~ 'Miss',
.$Title == 'Mme' ~ 'Mrs',
.$Title %in% rare_title ~ 'Rare Title',
TRUE ~ .$Title
))
set.seed(24)
full <- data.frame(Title = sample(c('Mlle', 'Ms', 'Mme', 'Colonel', 'Jr'), 20,
replace=TRUE), stringsAsFactors= FALSE)
rare_title <- 'Colonel'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With