Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't silently drop NA levels in strings2factors() #1291

Merged
merged 4 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* The `prefix` argument of `step_dummy_multi_choice()` is not properly documented. (#1298)

* `NA` levels in factors aren't dropped when passed to `recipe()`. (#1291)

# recipes 1.0.10

## Bug Fixes
Expand Down
6 changes: 4 additions & 2 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,11 @@ strings2factors <- function(x, info) {
for (i in seq_along(info)) {
lcol <- names(info)[i]
x[, lcol] <-
factor(as.character(x[[lcol]]),
factor(
as.character(x[[lcol]]),
levels = info[[i]]$values,
ordered = info[[i]]$ordered
ordered = info[[i]]$ordered,
exclude = NULL
)
}
x
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-basics.R
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,15 @@ test_that("`internal data is kept as tibbles when prepping", {
test_that("recipe() errors if `data` is missing", {
expect_snapshot(error = TRUE, recipe(mpg ~ .))
})

test_that("NAs aren't dropped in strings2factor() (#1291)", {
ex_data <- tibble(
x = factor(c("a", NA, "c"), exclude = NULL)
)

rec_res <- recipe(~., data = ex_data) %>%
prep() %>%
bake(new_data = NULL)

expect_identical(rec_res, ex_data)
})
Loading