Is a data frame name required when using case_when inside a mutant?

full <- full %>% mutate(Title = case_when( Title %in% c('Mlle', 'Ms') ~ 'Miss', Title == 'Mme' ~ 'Mrs', Title %in% rare_title ~ 'Rare Title', TRUE ~ Title )) 

The code above gives an error: Error in eval(substitute(expr), envir, enclos) : object 'Title' not found

However, the code below works. Is the name of the data frame 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 )) 
+5
source share
1 answer

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 )) 

data

 set.seed(24) full <- data.frame(Title = sample(c('Mlle', 'Ms', 'Mme', 'Colonel', 'Jr'), 20, replace=TRUE), stringsAsFactors= FALSE) rare_title <- 'Colonel' 
+3
source

Source: https://habr.com/ru/post/1259349/


All Articles