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

Preserve attributes in fct_drop? #83

Closed
dpavlic opened this issue Feb 10, 2017 · 4 comments
Closed

Preserve attributes in fct_drop? #83

dpavlic opened this issue Feb 10, 2017 · 4 comments
Labels

Comments

@dpavlic
Copy link

@dpavlic 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
Copy link
Member

@hadley 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
Copy link
Member

@hadley 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
Copy link
Member

@hadley hadley commented Feb 11, 2018

Fixed in 090160d

@hadley hadley closed this Feb 11, 2018
@tjtnew
Copy link

@tjtnew tjtnew commented Aug 21, 2019

I think this is still an issue as refactor overwrites the class with the reset result of factor:

forcats/R/lvls.R

Lines 92 to 100 in fb11604

refactor <- function(f, new_levels, ordered = NA) {
if (is.na(ordered)) {
ordered <- is.ordered(f)
}
new_f <- factor(f, levels = new_levels, exclude = NULL, ordered = ordered)
attributes(new_f) <- utils::modifyList(attributes(f), attributes(new_f))
new_f
}
I think this can be fixed by adding class(new_f) <- NULL in between lines 97 and 98. Happy to do a pull request if appropriate.

Example of current behaviour below

library(forcats, quietly = TRUE)
library(Hmisc, quietly = TRUE)

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)

# original attributes
attributes(a$a)
#> $levels
#> [1] "a" "b" "c"
#> 
#> $class
#> [1] "labelled" "factor"  
#> 
#> $label
#> [1] "Var a"

# new attributes - labelled class dropped
attributes(a$a_drop)
#> $levels
#> [1] "a" "b"
#> 
#> $class
#> [1] "factor"
#> 
#> $label
#> [1] "Var a"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants