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

`labelled_spss` metadata are not retained when saving SPSS files #334

Closed
huftis opened this Issue Jan 9, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@huftis
Copy link
Contributor

huftis commented Jan 9, 2018

When saving a tibble with labelled_spss variables (as opposed to just labelled variables) to an SPSS file, none of the metadata from the labelled_spss variables are actually saved.

Note that labelled_spss variables are automatically created when importing (read_spss()) SPSS files using the user_na = TRUE argument, so this bug makes round-tripping SPSS files not possible.

Here’s an reprex:

library(tibble)
library(haven)
 
# Create example data
x = c(1,2,1,9)
d = tibble(
  x1 = labelled(x, labels = c(no = 1, yes = 2, unknown = 9)),
  x2 = labelled_spss(x, labels = c(no = 1, yes = 2, unknown = 9), na_values = 9)
)
 
# Look at the data (all metadata are correctly stored)
str(d$x1)
#> Class 'labelled'  atomic [1:4] 1 2 1 9
#>   ..- attr(*, "labels")= Named num [1:3] 1 2 9
#>   .. ..- attr(*, "names")= chr [1:3] "no" "yes" "unknown"
str(d$x2)
#> Classes 'labelled_spss', 'labelled'  atomic [1:4] 1 2 1 9
#>   ..- attr(*, "labels")= Named num [1:3] 1 2 9
#>   .. ..- attr(*, "names")= chr [1:3] "no" "yes" "unknown"
#>   ..- attr(*, "na_values")= num 9

# Save the data
write_sav(d, "labs.sav")
 
# Read the data (in two different ways)
d2 = read_spss("labs.sav")
d3 = read_spss("labs.sav", user_na = TRUE)
 
# The x1 variables are OK
str(d2$x1)
#> Class 'labelled'  atomic [1:4] 1 2 1 9
#>   ..- attr(*, "format.spss")= chr "F8.2"
#>   ..- attr(*, "labels")= Named num [1:3] 1 2 9
#>   .. ..- attr(*, "names")= chr [1:3] "no" "yes" "unknown"
str(d3$x1)
#> Class 'labelled'  atomic [1:4] 1 2 1 9
#>   ..- attr(*, "format.spss")= chr "F8.2"
#>   ..- attr(*, "labels")= Named num [1:3] 1 2 9
#>   .. ..- attr(*, "names")= chr [1:3] "no" "yes" "unknown"

# But the x2 variables are now missing ~all their metadata
str(d2$x2)
#>  atomic [1:4] 1 2 1 9
#>  - attr(*, "format.spss")= chr "F8.2"
str(d3$x2)
#>  atomic [1:4] 1 2 1 9
#>  - attr(*, "format.spss")= chr "F8.2"

The reprex shows that the x1 data (labelled) are correctly saved and read, while the x2 data (labelled_spss) are not correctly saved. (The bug is in saving the SPSS file, not in reading the file. If I open the saved filed in SPSS, the metadata is missing. And if I manually add the metadata in SPSS and then save the file, the metadata is correctly imported in R with read_spss().)

I have tested above code using the latest Git version of the haven package (and the latest CRAN version).

@hadley hadley added the feature label Jan 16, 2018

hadley added a commit that referenced this issue Jan 16, 2018

@hadley hadley closed this in da43fd1 Jan 16, 2018

@lock

This comment has been minimized.

Copy link

lock bot commented Jul 15, 2018

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/

@lock lock bot locked and limited conversation to collaborators Jul 15, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.