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).
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_dropfor dropping unused levels, it adds NA as a factor level, which is then given a number withas.numeric.There's already the
fct_explicit_naif you want to add missing values as a factor level, so would you consider removing the feature fromfct_drop, or adding an argument for choosing either option?If you like to keep
drop_levelslike this, then it would be better to change the description, as the functioning ofdroplevelsandfct_dropare not the same in the case of missing values (the first hasexclude = NA, and the latterexclude = NULL).