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

fct_recode removes label attribute #346

Open
iago-pssjd opened this issue Feb 21, 2023 · 4 comments
Open

fct_recode removes label attribute #346

iago-pssjd opened this issue Feb 21, 2023 · 4 comments
Labels
reprex needs a minimal reproducible example

Comments

@iago-pssjd
Copy link

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

hadley commented Oct 31, 2023

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.

@hadley hadley added the reprex needs a minimal reproducible example label Oct 31, 2023
@iago-pssjd
Copy link
Author

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

hadley commented Oct 31, 2023

Thanks!

@DanChaltiel
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
reprex needs a minimal reproducible example
Projects
None yet
Development

No branches or pull requests

3 participants