tidyverse / forcats

🐈🐈🐈🐈: tools for working with categorical variables (factors)
https://forcats.tidyverse.org/
Other
554 stars 126 forks source link

`fct_unique()` improvements #328

Closed hadley closed 1 year ago

hadley commented 1 year ago
hadley commented 1 year ago

From @DavisVaughan:

For future readers, we discovered that while most factors replace NA with an integer code when NA is used as an explicit factor level, it is actually documented in ?factor that is.na<-.factor can be used to simultaneously have NA as a code and NA as a completely separate explicit factor level that maps to an integer code.

x <- factor(c("a", NA, NA, "b"), levels = c("a", "b", NA), exclude = NULL)
unclass(x)
#> [1] 1 3 3 2
#> attr(,"levels")
#> [1] "a" "b" NA

is.na(x)[3] <- TRUE

x
#> [1] a    <NA> <NA> b   
#> Levels: a b <NA>
unclass(x) # technically 3 and NA are different
#> [1]  1  3 NA  2
#> attr(,"levels")
#> [1] "a" "b" NA

So this test ensures we support that