-
Notifications
You must be signed in to change notification settings - Fork 16
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
Adding and removing labels should maybe change the class? #111
Comments
Hi. Thanks for your feedback. First of all, I have the feeling that there has been confusion between different concepts. Hmisc and haven handle labelled data differently. The class
library(tidyverse)
library(labelled)
library(haven)
var_label(iris$Sepal.Width) <- "Width of sepal"
# Adding a variable label do not change the class
class(iris$Sepal.Width)
#> [1] "numeric"
iris %>%
mutate(Sepal.Width=if_else(Sepal.Width>2, 2, Sepal.Width)) %>% head(1)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 2 1.4 0.2 setosa
# When importing a SAS file with haven, no labelled class added to vector
path <- system.file("examples", "iris.sas7bdat", package = "haven")
df <- read_sas(path)
df %>% look_for()
#> pos variable label col_type values
#> 1 Sepal_Length — dbl
#> 2 Sepal_Width — dbl
#> 3 Petal_Length — dbl
#> 4 Petal_Width — dbl
#> 5 Species — chr Created on 2021-11-24 by the reprex package (v2.0.1) |
The class |
Oh, indeed I messed up with my code and grew sure that However, the question is still open to me: if Do you know why haven's philosophy is to not add the class? Maybe I should even remove it from crosstable... |
As the Regarding The Regarding variable labels (i.e. labels attached to a variable, not to specific values), when imported by So far, a request for a specific class is only for changing the printing of a vector in the console. For that purpose, the gain is too small compared to the added complexity. There is no real need to systematically print the variable label in the console, because there are other tools more appropriate, tools that can be easily used by users when they want to display the variable labels. Variable labels are displayed in RStudio viewer. With To summary, assigning a dedicated class to vectors having a variable label adds a lot of complexity and risks of incompatibility with other packages / functions. The interest seems minimal (just changing printing in the console) and we already have other tools to display variable labels (often more nicely) to users. |
For creating nicely formatted summary tables, it is relevant to use variable labels when available. However, I do not have the feeling that you need the Hmisc labelled class for that. The |
Thank you for this clear and thorough explanation. Although I never use this package anymore, I learned about labels through You totally convinced me though, there is indeed really minimal interest in adding the |
Hi Joseph,
Thanks for this great package, the R community really needed a standard to work with labels.
BTW, I'm the creator of the package
crosstable
which has a lot of label-related functions, and I wanted to export those functions to a specific package when I discovered yours. I hope one day I can depend on your package instead, to avoid code redundancy.I'm facing a problem when using
labelled
in coordination withhaven
. In data read from SAS (I'm mostly usinghaven::read_xpt()
andhaven::read_sas()
), columns have alabelled
class. I think this makes a lot of sense because you can then design methods for variables instead of checking the attribute.However, as
haven
will not implementvctrs
methods forlabelled
variables for some reason (see tidyverse/haven#565), you can have some pesky errors when working withdplyr::mutate()
,tidyr::pivot_longer()
, and their friends. Removing labels withlabelled::remove_labels
will not avoid the error, but it would work if you remove the class (whichcrosstable::remove_labels()
do).Here is an example:
Created on 2021-11-24 by the reprex package (v2.0.1)
For more insight into what adding a class can bring, here are the
print.labelled()
functions fromHmisc
andexpss
:Code
Created on 2021-11-24 by the reprex package (v2.0.1)
If that is OK with you, I think removing the class in
remove_labels()
is safe.Adding a class in
var_label()
might require implementingvctrs
methods (link, example) if you want to be thorough, but I think it would add a significant value to your package.The text was updated successfully, but these errors were encountered: