forcats
forcats copied to clipboard
`fct_reorder()` treatment of missing values in .x depends on whether .f is a factor or a character vector
When .x contains missing values, this throws an error if .f is a character vector but not if .f is a factor (with no missing values in either case).
This behaviour is not documented (and I suspect is not intended). It occurs in v0.5.0 and v1.0.0.
Example:
.f <- c("a", "b", "c")
.x <- c(1, 2, NA)
forcats::fct_reorder(.f, .x)
forcats::fct_reorder(forcats::as_factor(.f), .x)
library(forcats)
.f <- c("a", "b", "c")
.x <- c(1, 2, NA)
fct_reorder(.f, .x)
#> Warning: `fct_reorder()` removing 1 missing value.
#> ℹ Use `.na_rm = TRUE` to silence this message.
#> ℹ Use `.na_rm = FALSE` to preserve NAs.
#> Error in `lvls_reorder()`:
#> ! `idx` must contain one integer for each level of `f`
fct_reorder(as_factor(.f), .x)
#> Warning: `fct_reorder()` removing 1 missing value.
#> ℹ Use `.na_rm = TRUE` to silence this message.
#> ℹ Use `.na_rm = FALSE` to preserve NAs.
#> [1] a b c
#> Levels: a b c
Created on 2024-10-21 with reprex v2.1.0
Probably check_factor() should use as_factor()