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

write_sav() fails to set missing values #596

Closed
dusadrian opened this issue May 5, 2021 · 4 comments · Fixed by #629
Closed

write_sav() fails to set missing values #596

dusadrian opened this issue May 5, 2021 · 4 comments · Fixed by #629

Comments

@dusadrian
Copy link

dusadrian commented May 5, 2021

Information about missing values is lost, roundtrip reprex below

library(haven)
library(tibble)
test <- tibble(A = labelled_spss(1:10, c(Good = 1, Bad = 8, DK = 9, NR = 10), na_values = c(9, 10)))
test$A
# <labelled_spss<integer>[10]>
#  [1]  1  2  3  4  5  6  7  8  9 10
# Missing values: 9, 10

# Labels:
#  value label
#      1  Good
#      8   Bad
#      9    DK
#     10    NR

write_sav(test, "test.sav")

testback <- read_sav("test.sav")
testback$A
# <labelled<double>[10]>
#  [1]  1  2  3  4  5  6  7  8  9 10

# Labels:
#  value label
#      1  Good
#      8   Bad
#      9    DK
#     10    NR
@dusadrian dusadrian changed the title write_sav() does not set missing values write_sav() fails to set missing values Jun 24, 2021
@gorcha
Copy link
Member

gorcha commented Aug 4, 2021

HI @dusadrian,

This appears to be an issue in readstat - it supports writing missing values for doubles, but not for integers.
It'll need to be fixed in readstat before we can do anything in haven, I'll raise an issue over there.

In the mean time, as a work around you can convert to numeric before making it a labelled vector:

library(haven)
library(tibble)
test <- tibble(A = labelled_spss(as.numeric(1:10), c(Good = 1, Bad = 8, DK = 9, NR = 10), na_values = c(9, 10)))
test$A
#> <labelled_spss<double>[10]>
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> Missing values: 9, 10
#> 
#> Labels:
#>  value label
#>      1  Good
#>      8   Bad
#>      9    DK
#>     10    NR

write_sav(test, "test.sav")

testback <- read_sav("test.sav", user_na = TRUE)
testback$A
#> <labelled_spss<double>[10]>
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> Missing values: 9, 10
#> 
#> Labels:
#>  value label
#>      1  Good
#>      8   Bad
#>      9    DK
#>     10    NR

Created on 2021-08-04 by the reprex package (v2.0.0)

@dusadrian
Copy link
Author

dusadrian commented Aug 4, 2021

Thanks @gorcha, that is a good workaround in the meantime. And hopefully, it will be solved in ReadStat soon.

@gorcha
Copy link
Member

gorcha commented Aug 4, 2021

Hi @dusadrian - sorry, to clarify I think this issue should stay open in haven.

This change will still need to be made in haven, it just needs to be implemented in readstat first 🙂

@dusadrian
Copy link
Author

Oh, I see. Alright then, I am reopening it to make sure it will be solved at some point.

@dusadrian dusadrian reopened this Aug 4, 2021
gorcha added a commit that referenced this issue Aug 18, 2021
…629)

* Add support for writing user missing values to integer vectors. Fixes #596
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants