-
Notifications
You must be signed in to change notification settings - Fork 76
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
rio 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. |
Thanks for your work. Unfortunately, it seems not to work as expected. if (!require("remotes")){ to install the latest version of rio. But the variablelabels still vanish. |
My bug seems related, though is not about exporting but simply importing a Stata-dataset. Was quite surprised to see that |
Could you @zahlenzauber please explain to me if this is the expected behavior? If it is the case, I would consider this issue is resolved. 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)
tempsav1 <- tempfile(fileext = ".sav")
rio::export(test, tempsav1)
test_rio <- rio::import(tempsav1)
str(test_rio)
#> 'data.frame': 3 obs. of 2 variables:
#> $ NoValueLabels: num 1 2 3
#> ..- attr(*, "label")= chr "Variablelabel, but without Valuelabels"
#> ..- attr(*, "format.spss")= chr "F8.2"
#> $ ValueLabels : num 1 2 3
#> ..- attr(*, "label")= chr "Variablelabel and Valuelabels"
#> ..- attr(*, "format.spss")= chr "F8.2"
#> ..- attr(*, "labels")= Named num [1:3] 1 2 3
#> .. ..- attr(*, "names")= chr [1:3] "Valuelabel 1" "Valuelabel 2" "Valuelabel 3"
attr(test_rio$ValueLabels, "labels")
#> Valuelabel 1 Valuelabel 2 Valuelabel 3
#> 1 2 3
sessionInfo()
#> R version 4.3.1 (2023-06-16)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 22.04.3 LTS
#>
#> Matrix products: default
#> BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
#>
#> locale:
#> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
#> [3] LC_TIME=de_DE.UTF-8 LC_COLLATE=en_US.UTF-8
#> [5] LC_MONETARY=de_DE.UTF-8 LC_MESSAGES=en_US.UTF-8
#> [7] LC_PAPER=de_DE.UTF-8 LC_NAME=C
#> [9] LC_ADDRESS=C LC_TELEPHONE=C
#> [11] LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C
#>
#> time zone: Europe/Berlin
#> tzcode source: system (glibc)
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> loaded via a namespace (and not attached):
#> [1] compiler_4.3.1 reprex_2.0.2 Rcpp_1.0.11 zip_2.3.0
#> [5] readxl_1.4.3 yaml_2.3.7 fastmap_1.1.1 R6_2.5.1
#> [9] readr_2.1.4 curl_5.0.2 openxlsx_4.2.5.2 knitr_1.43
#> [13] forcats_1.0.0 tibble_3.2.1 R.cache_0.16.0 tzdb_0.4.0
#> [17] pillar_1.9.0 R.utils_2.12.2 rlang_1.1.1 utf8_1.2.3
#> [21] stringi_1.7.12 xfun_0.40 fs_1.6.3 cli_3.6.1
#> [25] withr_2.5.0 magrittr_2.0.3 rio_0.5.30 digest_0.6.33
#> [29] haven_2.5.3 hms_1.1.3 lifecycle_1.0.3 R.methodsS3_1.8.2
#> [33] R.oo_1.25.0 vctrs_0.6.3 evaluate_0.21 glue_1.6.2
#> [37] data.table_1.14.8 cellranger_1.1.0 styler_1.10.1 fansi_1.0.4
#> [41] foreign_0.8-82 rmarkdown_2.24 purrr_1.0.2 tools_4.3.1
#> [45] pkgconfig_2.0.3 htmltools_0.5.6 Created on 2023-08-31 with reprex v2.0.2 |
Dear @chainsawriot thanks a lot! It's great and the str()-output looks as expected now. |
@zahlenzauber Thanks a lot! |
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
The text was updated successfully, but these errors were encountered: