tidyverse / forcats

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

`fct_infreq()` doesn't reorder NA levels #326

Closed hadley closed 1 year ago

hadley commented 1 year ago
library(dplyr, warn.conflicts = FALSE)
library(forcats)

starwars |> 
  mutate(hair_color = fct_infreq(hair_color)) |> 
  count(hair_color) |> 
  filter(n > 1)
#> # A tibble: 6 × 2
#>   hair_color     n
#>   <fct>      <int>
#> 1 none          37
#> 2 brown         18
#> 3 black         13
#> 4 white          4
#> 5 blond          3
#> 6 <NA>           5

Created on 2023-01-03 with reprex v2.0.2

hadley commented 1 year ago

It can't reorder implicit NAs, so we probably need to document this and show how to convert to explicit.