strengejacke / sjlabelled

Working with Labelled Data in R

Home Page:https://strengejacke.github.io/sjlabelled

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

write_spss error message when there are value labels

zahlenzauber opened this issue · comments

Dear Daniel Lüdecke,
thanks for your package, that helps when working with R and SPSS in one team.
After an update to R version 4.0.1 (2020-06-06) there is an issue in a special case.
I get an error for write_spss if there are value labels. Without value labels everything is still fine.

By the way, there is a similar issue with the rio-package. Maybe this will help to narrow down the problem.

Kind regards
Thomas

#MWE
#Errormessage exporting SAV when there are value labels

rm(list=ls())

variablename <- as.numeric(c(1:3))

test <- data.frame(variablename)

attr(test$variablename, "label") <- "Variablelabel"

with this attribute there is an error

attr(test$variablename, "labels") <- c("Valuelabel 1"=1, "Valuelabel 2"=2, "Valuelabel 3"=3)

sjlabelled::write_spss(test, "test.sav")

without this attribute there is no error

attr(test$variablename, "labels") <- NULL

sjlabelled::write_spss(test, "test.sav")

sessionInfo()
R version 4.0.1 (2020-06-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C
[5] LC_TIME=German_Germany.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] tidyr_1.1.0 dplyr_1.0.0 stringr_1.4.0 sjlabelled_1.1.5

loaded via a namespace (and not attached):
[1] tidyselect_1.1.0 xfun_0.14 purrr_0.3.4
[4] pander_0.6.3 lattice_0.20-41 haven_2.3.1
[7] tcltk_4.0.1 vctrs_0.3.1 summarytools_0.9.6
[10] generics_0.0.2 htmltools_0.4.0 yaml_2.2.1
[13] base64enc_0.1-3 rlang_0.4.6 pillar_1.4.4
[16] foreign_0.8-80 glue_1.4.1 pryr_0.1.4
[19] readxl_1.3.1 matrixStats_0.56.0 lifecycle_0.2.0
[22] plyr_1.8.6 sjmisc_2.8.5 cellranger_1.1.0
[25] zip_2.0.4 codetools_0.2-16 psych_1.9.12.31
[28] knitr_1.28 rio_0.5.16 forcats_0.5.0
[31] curl_4.3 parallel_4.0.1 Rcpp_1.0.4.6
[34] readr_1.3.1 backports_1.1.7 checkmate_2.0.0
[37] magick_2.3 tmvnsim_1.0-2 rapportools_1.0
[40] mnormt_2.0.0 hms_0.5.3 digest_0.6.25
[43] stringi_1.4.6 openxlsx_4.1.5 insight_0.8.5
[46] grid_4.0.1 tools_4.0.1 magrittr_1.5
[49] tibble_3.0.1 crayon_1.3.4 pkgconfig_2.0.3
[52] ellipsis_0.3.1 data.table_1.12.8 lubridate_1.7.9
[55] rstudioapi_0.11 R6_2.4.1 nlme_3.1-148
[58] compiler_4.0.1

May be this helps. It seems to me that "haven" changed the way the attributes are set. When you write and read the data with haven we have the same effect for the MWE above. But if the labelling uses haven::labelled_spss the labels are kept (as long as you use haven::write_sav).

Here is an MWE, that uses rio:

rm(list=ls())

ValueLabels <- as.numeric(c(1:3))
NoValueLabels <- as.numeric(c(1:3))
HavenLabels <- as.numeric(c(1:3))

test <- data.frame(NoValueLabels, ValueLabels, HavenLabels)

attr(test$NoValueLabels, "label") <- "Variablelabel, but without Valuelabels"
attr(test$ValueLabels, "label") <- "Variablelabel and Valuelabels"

attr(test$ValueLabels, "labels") <- c("Valuelabel 1"=1, "Valuelabel 2"=2, "Valuelabel 3"=3)

test$HavenLabels <- haven::labelled_spss(test$HavenLabels, labels=c("Valuelabel 1"=1, "Valuelabel 2"=2, "Valuelabel 3"=3), label = "Variable and Valuelabels with haven" )

rio::export(test, "test.sav")
reimportTest <- rio::import("test.sav")

haven::write_sav(test, "testHaven.sav")
reimportTestHaven <- haven::read_sav("testHaven.sav")

str(test)
str(reimportTest)
str(reimportTestHaven)

Can you install from GitHub and check if a) you can write data to SPSS and b) import these exported data into SPSS?

Hallo Daniel,
now it works as expected. Thanks a lot for the fast reaction.
Thomas

great, easier than expected... :-)

...anyway, it was definitely easier for me ;-)
Thanks again