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

Copy value and variable labels to MERGED data frames

nclsbarreto opened this issue · comments

Hi,

I am having trouble migrating the variable labels from two smaller datasets to a merged form of the dataset. when i merge the data frames (technically i have them as tibbles) the variable lebels are dropped. How do i go about attaching the variable labels back on to the variables in the new, merged data frame?

Below is my session info as well as example of the code i am trying to run. I can't give any data for security reasons, but any help/suggestions/directions would be helpful as this issue will keep coming up.

carroll1 <- as_tibble(read.spss("../raw data/Carroll data/tsfdis.master11.26.2018.sav",
header = T, to.data.frame = T))
carroll2 <- as_tibble(read.spss("../raw data/Carroll data/tsfdis.humphreys.03.05.2019.sav",
header = T, to.data.frame = T))

test <- rbind(cbind(attr(carroll1, "variable.labels")),
cbind(attr(carroll2, "variable.labels")[-1]))

carroll <- copy_labels(carroll, test1)

both the following do not show that any variable labels have been made.
attributes(carroll)
attr(carroll,"variable.labels")

R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252

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

other attached packages:
[1] sjlabelled_1.0.17 labelled_2.2.1 Hmisc_4.2-0 Formula_1.2-3 survival_2.44-1.1 lattice_0.20-38 sas7bdat_0.5 forcats_0.3.0
[9] stringr_1.3.1 dplyr_0.8.1 purrr_0.2.5 readr_1.3.0 tidyr_0.8.2 tibble_2.1.1 ggplot2_3.1.0 tidyverse_1.2.1
[17] tableone_0.10.0 arm_1.10-1 lme4_1.1-21 Matrix_1.2-14 MASS_7.3-51.4 expss_0.8.11 foreign_0.8-71 lavaan_0.6-3
[25] psych_1.8.10

loaded via a namespace (and not attached):
[1] nlme_3.1-137 matrixStats_0.54.0 lubridate_1.7.4 insight_0.3.0 RColorBrewer_1.1-2 httr_1.4.0 tools_3.5.1
[8] backports_1.1.4 utf8_1.1.4 R6_2.4.0 rpart_4.1-15 DBI_1.0.0 lazyeval_0.2.2 colorspace_1.4-1
[15] nnet_7.3-12 withr_2.1.2 tidyselect_0.2.5 gridExtra_2.3 mnormt_1.5-5 compiler_3.5.1 cli_1.1.0
[22] rvest_0.3.2 htmlTable_1.13.1 xml2_1.2.0 scales_1.0.0 checkmate_1.9.3 digest_0.6.18 pbivnorm_0.6.0
[29] minqa_1.2.4 base64enc_0.1-3 pkgconfig_2.0.2 htmltools_0.3.6 htmlwidgets_1.3 rlang_0.3.4 readxl_1.3.1
[36] rstudioapi_0.10 generics_0.0.2 jsonlite_1.6 acepack_1.4.1 magrittr_1.5 Rcpp_1.0.1 munsell_0.5.0
[43] fansi_0.4.0 abind_1.4-5 stringi_1.4.3 yaml_2.2.0 plyr_1.8.4 grid_3.5.1 parallel_3.5.1
[50] crayon_1.3.4 haven_2.1.0 splines_3.5.1 hms_0.4.2 zeallot_0.1.0 knitr_1.22 pillar_1.4.0
[57] boot_1.3-22 stats4_3.5.1 glue_1.3.1 mitools_2.4 latticeExtra_0.6-28 data.table_1.12.2 modelr_0.1.4
[64] vctrs_0.1.0 nloptr_1.2.1 cellranger_1.1.0 gtable_0.2.0 assertthat_0.2.1 xfun_0.7 broom_0.5.2
[71] survey_3.36 coda_0.19-2 cluster_2.0.9

Do you have a reproducible example?

I can't due to the data being private (I'd have to make random examples). I actually think i figured it out. Using the tidyverse set of packages (i.e. haven) to pull in the data saves the labels as "label" in the attr of the data.frame or tibble. when you use foreign it saves them as "variable.labels" so the calls from sjlabelled dont work.

Yes that might be the reason. You can use sjlabelled::read_spss() to read the data.