tidyverse / forcats

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

fct_recode removes `label` attribute #346

Open iago-pssjd opened 1 year ago

iago-pssjd commented 1 year ago

Hi!

fct_recode removes label attribute (and seems that it removes all the attributes). I expect it would not do (it may have sense that it removes a labels attribute, but not others).

Reprex:

library(haven)
library(forcats)
path <- system.file("examples", "iris.dta", package = "haven")
read_dta(path) |> str()
...
...
...
$ species    : chr [1:150] "setosa" "setosa" "setosa" "setosa" ...
  ..- attr(*, "label")= chr "Species"
  ..- attr(*, "format.stata")= chr "%10s"
# However
read_dta(path)$species |> fct_recode(seto = "setosa") |> str()
 Factor w/ 3 levels "seto","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
hadley commented 10 months ago

Could you please rework your reproducible example to use the reprex package ? That makes it easier to see both the input and the output, formatted in such a way that I can easily re-run in a local session.

iago-pssjd commented 10 months ago

@hadley done:

library(haven)
library(forcats)
path <- system.file("examples", "iris.dta", package = "haven")
t <- read_dta(path) 
t$species |> str()
#>  chr [1:150] "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" ...
#>  - attr(*, "label")= chr "Species"
#>  - attr(*, "format.stata")= chr "%10s"
t$species |> fct_recode(seto = "setosa") |> str()
#>  Factor w/ 3 levels "seto","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

Created on 2023-10-31 with reprex v2.0.2

hadley commented 10 months ago

Thanks!

DanChaltiel commented 3 months ago

It seems this is not limited to fct_recode(), as I experience the same with most forcats functions. This feature would be much appreciated by those like me that rely a lot on attributes like label.