data(Sacramento, package = "modeldata")
set.seed(19)
in_train <- sample(1:nrow(Sacramento), size = 800)
sacr_tr <- Sacramento[in_train, ]
sacr_te <- Sacramento[-in_train, ]
rec <- recipe(~ city + zip, data = sacr_tr)
rec <- rec %>%
step_other(city, zip, threshold = .1, other = "other values")
rec <- prep(rec, training = sacr_tr)
collapsed <- bake(rec, sacr_te)
table(sacr_te$city, collapsed$city, useNA = "always")
tidy(rec, number = 1)
# novel levels are also "othered"
tahiti <- Sacramento[1, ]
tahiti$zip <- "a magical place"
bake(rec, tahiti)
# threshold as a frequency
rec <- recipe(~ city + zip, data = sacr_tr)
rec <- rec %>%
step_other(city, zip, threshold = 2000, other = "other values")
rec <- prep(rec, training = sacr_tr)
tidy(rec, number = 1)
# compare it to
# sacr_tr %>% count(city, sort = TRUE) %>% top_n(4)
# sacr_tr %>% count(zip, sort = TRUE) %>% top_n(3)
Run the code above in your browser using DataLab