Skip to content

fix: fct_na_value_to_level() no longer adds unnecessary level when no NAs present (#347)#398

Open
LeonidasZhak wants to merge 2 commits into
tidyverse:mainfrom
LeonidasZhak:fix/fct-na-value-to-level-unnecessary-level-347
Open

fix: fct_na_value_to_level() no longer adds unnecessary level when no NAs present (#347)#398
LeonidasZhak wants to merge 2 commits into
tidyverse:mainfrom
LeonidasZhak:fix/fct-na-value-to-level-unnecessary-level-347

Conversation

@LeonidasZhak

Copy link
Copy Markdown

Summary

Fixes #347

When called on a factor with no NA values, fct_na_value_to_level() previously added an unnecessary level (either NA or the custom level). Now it returns the factor unchanged, matching the behavior of the deprecated fct_explicit_na().

Changes

  • R/na.R: Added early return when f contains no NA values
  • tests/testthat/test-na.R: Added test verifying the fix

Reprex

Before:

library(forcats)
letters[1:3] |> factor() |> fct_na_value_to_level("x") |> levels()
# [1] "a" "b" "c" "x"  # unexpected: "x" level added

After:

library(forcats)
letters[1:3] |> factor() |> fct_na_value_to_level("x") |> levels()
# [1] "a" "b" "c"  # correct: no change when no NAs

Tests

All 225 forcats tests pass, including the new test.

…yverse#387)

- Add input validation for .x when using default median function
- Provide clear error message suggesting numeric .x or custom .fun
- Add tests for character and factor .x inputs
- Add test showing custom .fun works with character .x
… NAs present (tidyverse#347)

When called on a factor with no NA values, fct_na_value_to_level() previously
added an unnecessary level (either NA or the custom level). Now it returns the
factor unchanged, matching the behavior of the deprecated fct_explicit_na().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fct_na_value_to_level() adds a level for NA value even if it is not need

1 participant