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
bug an unexpected problem or unintended behavior

Comments

@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
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 as completed Feb 10, 2018
@hadley
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 an unexpected problem or unintended behavior label Feb 11, 2018
@hadley
Copy link
Member

hadley commented Feb 11, 2018

Fixed in 090160d

@hadley hadley closed this as completed Feb 11, 2018
@TimTaylor
Copy link

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
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

3 participants