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

Preserve attributes in fct_drop? #83

Closed
dpavlic opened this Issue Feb 10, 2017 · 3 comments

Comments

Projects
None yet
2 participants
@dpavlic
Copy link

dpavlic commented Feb 10, 2017

One of the ways in which fct_drop is very much the same as droplevels is that it removes attributes (such as Hmisc labels) when it drops extra factor levels:

library(Hmisc)
library(forcats)

a <- data.frame(a = factor(c('a', 'b', 'c')), b = c(1:3))
a <- a[1:2, ]
label(a$a) <- 'Var a'
a$a_drop <- fct_drop(a$a)

They are there in the original:

> attributes(a$a)
$levels
[1] "a" "b" "c"

$class
[1] "labelled" "factor"  

$label
[1] "Var a"

Gone in the drop:

> attributes(a$a_drop)
$levels
[1] "a" "b"

$class
[1] "factor"

Would it be possible to preserve them?

@hadley

This comment has been minimized.

Copy link
Member

hadley commented Feb 10, 2018

I can't see any easy way to make this happen without a bunch of work unfortunately. fct_drop() has to create a new factor, and there's no obviously way to make this generic.

@hadley hadley closed this Feb 10, 2018

@hadley

This comment has been minimized.

Copy link
Member

hadley commented Feb 11, 2018

Hmmm, maybe this is the point of the refactor() function

@hadley hadley reopened this Feb 11, 2018

@hadley hadley added the bug label Feb 11, 2018

@hadley

This comment has been minimized.

Copy link
Member

hadley commented Feb 11, 2018

Fixed in 090160d

@hadley hadley closed this Feb 11, 2018

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