From f38c4de260848f3be76e5c0148b095505ccd02fb Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Wed, 30 Oct 2024 09:14:55 -0700 Subject: [PATCH] fix bug in step_clean_levels --- NEWS.md | 4 +++- R/clean_levels.R | 11 ++++++++--- tests/testthat/test-clean_levels.R | 1 + 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index 66c4b897..9a4821c6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,7 +4,9 @@ * Calling `?tidy.step_*()` now sends you to the documentation for `step_*()` where the outcome is documented. (#261) -* `step_textfeatures()` has been made faster and more robust. #265 +* `step_textfeatures()` has been made faster and more robust. (#265) + +* Fixed bug in `step_clean_levels()` where it would produce NAs for character columns. (#274) # textrecipes 1.0.6 diff --git a/R/clean_levels.R b/R/clean_levels.R index b1920aa0..c6f8683e 100644 --- a/R/clean_levels.R +++ b/R/clean_levels.R @@ -131,9 +131,14 @@ bake.step_clean_levels <- function(object, new_data, ...) { } for (col_name in col_names) { - new_data[[col_name]] <- dplyr::recode_factor( - new_data[[col_name]], !!!object$clean[[col_name]] - ) + if (is.factor(new_data[[col_name]])) { + new_data[[col_name]] <- dplyr::recode_factor( + new_data[[col_name]], !!!object$clean[[col_name]] + ) + } else { + new_data[[col_name]] <- janitor::make_clean_names(new_data[[col_name]]) + + } } new_data diff --git a/tests/testthat/test-clean_levels.R b/tests/testthat/test-clean_levels.R index 77af2c6e..f7ad597e 100644 --- a/tests/testthat/test-clean_levels.R +++ b/tests/testthat/test-clean_levels.R @@ -22,6 +22,7 @@ test_that("character input", { cleaned_te <- bake(cleaned, new_data = smith_te) expect_equal(sum(grepl(" ", cleaned_tr$name)), 0) + expect_equal(sum(is.na(cleaned_tr$name)), 0) expect_equal(sum(levels(cleaned_tr$name) %in% smith_tr$name), 0) tidy_exp_tr <- tibble(