Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign uprio loses "label" attributes if a "labels" attribute also exists when roundtripping to ".sav" and ".dta". #268
Comments
Can you check the version from github please? Thanks for the report! |
Hello Thomas Leeper, By the way, after updating sjlabelled::write_spss does not even export anymore. Maybe this will help narrow down the problem. Kind regards Here is my MWE: # MWE
# # when there are value labels (attribute "labels"), the variable label (attribute "label") is lost in rio::export to SPSS
rm(list=ls())
ValueLabels <- as.numeric(c(1:3))
NoValueLabels <- as.numeric(c(1:3))
test <- data.frame(NoValueLabels, ValueLabels )
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)
rio::export(test, "test.sav")
reimportTest <- rio::import("test.sav")
str(test)
str(reimportTest)
# compare test vs. reimportTest attribute "label":
# when there are value labels (attr "labels"), the variable labels (attr "label") is lost in rio::export to SPSS
#sessionInfo() Here is my 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 the extended MWE: 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) |
Thanks - I'll try to get to this as soon as possible. |
Thanks to you. Maybe this helps to find the solution. |
Thanks. This is definitely a bug. Working on a fix now. |
Just pushed to github - if you have time, let me know if that's now working as expected. |
rubenarslan commentedApr 22, 2020
•
edited
The title says it all. For some reason, rio doesn't write the variable label attribute to SPSS/Stata files, if there is also a "labels" attribute (value labels). This wasn't always the case, but I can't say what version introduced the bug.
Created on 2020-04-22 by the reprex package (v0.3.0)
Session info