tidyverse / forcats

🐈🐈🐈🐈: tools for working with categorical variables (factors)
https://forcats.tidyverse.org/
Other
553 stars 126 forks source link

`fct_drop()` and friends drop attributes when applied to character #322

Closed DanChaltiel closed 1 year ago

DanChaltiel commented 1 year ago

Hi,

This is a minor bug that can be annoying when working with labels. When using forcats modifiers like fct_drop() on factors, everything is fine, but if you use it on a character vector it removes all attributes (such as labels):

library(forcats)
library(expss)

fct("a") %>% set_var_lab("foo") %>% fct_drop() %>% var_lab()
#> [1] "foo"
"a" %>% set_var_lab("foo") %>% fct_drop() %>% var_lab()
#> NULL

Created on 2022-12-02 with reprex v2.0.2

Actually, this might be easily fixed by changing factor() to forcats::fct() in forcats:::check_factor() (https://github.com/tidyverse/forcats/blob/main/R/utils.R#L16).

hadley commented 1 year ago

You're converting an object from one type to another; I don't think there's a string a priori motivation for preserving attributes here.