haven icon indicating copy to clipboard operation
haven copied to clipboard

Class of haven_labelled/vctrs_vctr/character not being retained

Open ds-turner opened this issue 1 year ago • 1 comments

When I write an xpt file then read it back in, variables that were labelled do not retain the haven_labelled class and therefore drop the labels.

iris <- iris |>
  janitor::clean_names() |>
  dplyr::mutate(
    across(where(is.factor), labelled::to_labelled)
  )

tmp <- tempfile(fileext = ".xpt")
haven::write_xpt(iris, tmp)
iris2 <- haven::read_xpt(tmp)

waldo::compare(
  iris,
  iris2
)

`class(old)`: "data.frame"                   
`class(new)`: "tbl_df"     "tbl" "data.frame"

`old$species` is an S3 object of class <haven_labelled/vctrs_vctr/double>, a double vector
`new$species` is a double vector (1, 1, 1, 1, 1, ...)
R version 4.3.1 (2023-06-16 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22631)

 [1] crayon_1.5.3      vctrs_0.6.5       cli_3.6.3         rlang_1.1.5       stringi_1.8.4    
 [6] forcats_1.0.0     diffobj_0.3.5     haven_2.5.4       generics_0.1.3    glue_1.8.0       
[11] janitor_2.2.1     hms_1.1.3         snakecase_0.11.1  tibble_3.2.1      tzdb_0.4.0       
[16] lifecycle_1.0.4   stringr_1.5.1     compiler_4.3.1    dplyr_1.1.4       waldo_0.6.1      
[21] timechange_0.3.0  pkgconfig_2.0.3   rstudioapi_0.17.1 labelled_2.14.0   R6_2.6.1         
[26] readr_2.1.5       tidyselect_1.2.1  pillar_1.10.1     magrittr_2.0.3    tools_4.3.1      
[31] withr_3.0.2       lubridate_1.9.4  

ds-turner avatar Mar 04 '25 11:03 ds-turner

I'm not 100% sure but I think this is expected behaviour. XPT files do not store any value level formats/informats. Typically in SAS that is done by the formats catalog which XPT does not directly support.

gowerc avatar Mar 05 '25 21:03 gowerc