Skip to content
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

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

Closed
satuhelske opened this issue Nov 3, 2016 · 2 comments
Closed

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

satuhelske opened this issue Nov 3, 2016 · 2 comments

Comments

@satuhelske
Copy link

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).

@nickresnick
Copy link

(bump)

I agree with OP, fct_drop should only ever drop levels, even in the case of missing values.

@hadley
Copy link
Member

hadley commented Dec 30, 2016

Related to #69 (as need to completely rework implementation)

@hadley hadley closed this as completed in 81ba4d1 Dec 30, 2016
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

No branches or pull requests

3 participants