Skip to content

write_sav issues with labelled class vectors #193

Closed

Description

I have found some issues when saving labelled data to SPSS. I used the latest dev-build from 25th June. First, here is the reproducible example for the issues:

library(haven)
x <- labelled(c(1:3, tagged_na("a", "c", "z"), 4:1),
              c("Agreement" = 1, "äquivalent" = 2, "Disagreement" = 4, "First" = tagged_na("c"),
                "Refused" = tagged_na("a"), "Not home" = tagged_na("z")))

attr(x, "label") <- "Äquivalenzeinkommen"
x
# test.sav
haven::write_sav(data.frame(test = x), "test.sav")
# test2.sav
haven::write_sav(data.frame(test = haven::as_factor(x)), "test2.sav")

Now to what I have found:

  • Using the data.frame method on a labelled vector produces a column name with dots at the end. Dots are no legal characters at the end of variable names in SPSS. Trying to run any command with this variable leads to an error in SPSS.
> data.frame(x)
   x..i..
1       1
2       2
3       3
4      NA

spss_2

  • It seems that the "tags" of tagged NA values are saved, but recognized as invalid value labels. See screenshot and also the screenshot above:

spss_1

  • After changing the variable name, the value labels are not correctly assigned. See screenshot above, where 1 is agreement, 2 is äquivalenz and 4 is disagreement. Running the frequency command in SPSS gives following:

spss_4

  • After editing some of the value labels, assignment still is not correct:

spss_5

To summarise, labelled vectors are not properly saved to SPSS. However, factors are. The second SPSS file, where I used haven::as_factor(x)), seems to work, except that as_factor does not convert values to NA (thus, they are counted as regular values).

  • Correct value labels

spss_try2_2

  • Correct frequencies

spss_try2_1

  1. I would suggest to convert any labelled vector to factor with as_factor before writing the data.
  2. Furthermore, it would be nice to include an option, where (tagged/labelled) NA values can be converted to NA. You have to be sure to convert them to regular NA (i.e. removing the "tag" from the NA value), else you have broken value labels (see 1st/2nd screenshot at top)

Test1 and 2.zip

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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions