-
Notifications
You must be signed in to change notification settings - Fork 116
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
Subsetting loses variable labels #392
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Ah, I see. The reason for this bug is that subsetting the variables loses their attributes. And the reason for this is that the variables with only a Note that this only applied to variables that have a A possible solution would be for the importer to store the variables as Here’s a reprex: library(haven)
d <- read_sav("https://www.sheffield.ac.uk/polopoly_fs/1.547009!/file/Titanic_2.sav")
class(d$Residence) # OK
#> [1] "labelled"
class(d$fare) # Missing class
#> [1] "numeric"
# Subsetting ‘fare’ loses the attribute
attr(d$fare, "label")
#> [1] "Cost of ticket"
attr(d$fare[1:3], "label")
#> NULL
# But subsetting ‘Residence’ works fine
attr(d$Residence, "label")
#> [1] "Country of residence"
attr(d$Residence[1:3], "label")
#> [1] "Country of residence" |
This comment has been minimized.
This comment has been minimized.
I think the proposal to always use Instead I think you're better off creating a custom restore labels functions, something like this (untested, but should be close): restore_labels <- function(df, orig) {
for (var in names(original)) {
if (is.null(df[[var]]))
next
if (!is.null(attr(df[[var]], "label")))
next
if (is.null(attr(orig[[var]], "label")))
next
attr(df[[var]], "label") <- attr(orig[[var]], "label")
}
df
} |
|
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/ |
When I import SPSS data with read_sav() variable labels are correctly imported.
Then I create multiple subsamples of it and write new SPSS data frames with write_sav().
When opening the new dataframes in SPSS -or reading them back with read_sav()- variable labels are lost. Is there a way of fixing this?
Thanks!
The text was updated successfully, but these errors were encountered: