Skip to content

fct_inorder() fails if factor contains empty levels #262

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

Closed
jwilliman opened this issue Apr 29, 2020 · 2 comments · Fixed by #329
Closed

fct_inorder() fails if factor contains empty levels #262

jwilliman opened this issue Apr 29, 2020 · 2 comments · Fixed by #329
Labels
bug an unexpected problem or unintended behavior
Milestone

Comments

@jwilliman
Copy link

Running forcats::fct_inorder on factors with empty levels produces an (informative) error.

Is this behaviour expected/desired, or should empty levels be automatically dropped (with a warning)? If expected, would be good to flag on the help file.

library(forcats)
f <- factor(rev(letters[1:5]), levels = letters[1:6])
fct_inorder(f)
#> Error: `idx` must contain one integer for each level of `f`
fct_infreq(f)
#> [1] e d c b a
#> Levels: a b c d e f
fct_inorder(fct_drop(f))
#> [1] e d c b a
#> Levels: e d c b a

Created on 2020-04-29 by the reprex package (v0.3.0)

@hadley hadley added the bug an unexpected problem or unintended behavior label Dec 9, 2020
@hadley
Copy link
Member

hadley commented Dec 9, 2020

Hmmm, given that the levels don't appear in the data, what do you expect fct_inorder() to do with them? Put them at the end? Drop them?

@jwilliman
Copy link
Author

Put them at the end makes the most sense to me, as you may want them for zero counts in tables etc. You also have more flexibility keeping them, as you can simply run fct_drop if you don't want them, but if the default is to drop the levels then it is harder to add them back should you want them.

@hadley hadley added this to the v1.0.0 milestone Jan 3, 2023
@hadley hadley changed the title fct_inorder fails if factor contains empty levels fct_inorder() fails if factor contains empty levels Jan 3, 2023
hadley added a commit that referenced this issue Jan 3, 2023
Fixes #262.

Also improved test organisation and ensure the fct_inorder() and fct_infreq() have parallel tests.
hadley added a commit that referenced this issue Jan 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants