Skip to content

fct_drop not to add NA as a factor level? #52

Description

@satuhelske

I'm working with factors a lot, so forcats is a very welcome new package to me; nice and easy-to-use functions for describing and visualizing factors. However, when manipulating factors for other types of use, I'm finding it difficult that NA is sometimes added as an additional factor level. E.g., I have a factor with multiple different codes for missingness that I'd like to convert into a numerical variable, combining all missing values into NA. After recoding of missing values, if I use fct_drop for dropping unused levels, it adds NA as a factor level, which is then given a number with as.numeric.

> my_factor <- factor(c(letters[1:4], NA, NA), levels = letters[1:6])
> my_factor
[1] a    b    c    d    <NA> <NA>
Levels: a b c d e f
> as.numeric(my_factor)
[1]  1  2  3  4 NA NA
> fct_drop(my_factor)
[1] a    b    c    d    <NA> <NA>
Levels: a b c d <NA>
> as.numeric(fct_drop(my_factor))
[1] 1 2 3 4 5 5

There's already the fct_explicit_na if you want to add missing values as a factor level, so would you consider removing the feature from fct_drop, or adding an argument for choosing either option?

If you like to keep drop_levels like this, then it would be better to change the description, as the functioning of droplevels and fct_drop are not the same in the case of missing values (the first has exclude = NA, and the latter exclude = NULL).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions