-
Notifications
You must be signed in to change notification settings - Fork 111
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
fct_na_value_to_level does not work in recipes #1290
Comments
Hello @joranE 👋 To do this task we recommend that you use library(recipes)
ex_data <-
data.frame(
y = 1:3,
x = factor(c("a",NA,"c"))
)
rec <-
recipe(y~x,data = ex_data) |>
step_unknown(x)
rec_baked <-
rec |>
prep() |>
bake(new_data = NULL)
levels(rec_baked$x)
#> [1] "a" "c" "unknown" |
Thanks @EmilHvitfeldt ! I did actually know about Good to know that My discovery does make me sort of nervous now, though, that |
There are two things happening here: Firstly, this uncovered a bug in Secondly,
This can be seen here: library(forcats)
x <- factor(c("a",NA,"c"))
x_old <- fct_explicit_na(x)
x_new <- fct_na_value_to_level(x)
x_old
#> [1] a (Missing) c
#> Levels: a c (Missing)
x_new
#> [1] a <NA> c
#> Levels: a c <NA> Where this distinction is useful is that you are able to reverse is.na(x_old)
#> [1] FALSE FALSE FALSE
is.na(x_new)
#> [1] FALSE FALSE FALSE
x_old_rev <- fct_na_level_to_value(x_old)
x_new_rev <- fct_na_level_to_value(x_new)
is.na(x_old_rev)
#> [1] FALSE FALSE FALSE
is.na(x_new_rev)
#> [1] FALSE TRUE FALSE Regardless, you should be using |
This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex https://reprex.tidyverse.org) and link to this issue. |
I can't tell why, but it seems as though
forcats::fct_na_value_to_level
does not work when applied in recipes, specifically instep_mutate
. I discovered this when updating some code in response to the deprecation warning generated byforcats::fct_explicit_na
that says to usefct_na_value_to_level
instead.Reprex demonstrating the issue below. In short, both
fct_na_value_to_level
&fct_explicit_na
work as expected in isolation, but the newer version seems to do nothing when run insidestep_mutate
.I tried debugging
fct_na_value_to_level
when running baking the recipe, and it enters the function and appears to generate a factor with a new level and then exits the function fine, so I presume the result is just being discarded somehow elsewhere in the recipe machinery.Created on 2024-03-16 with reprex v2.0.2
Session info
The text was updated successfully, but these errors were encountered: