Skip to content

Class of haven_labelled/vctrs_vctr/character not being retained #773

@ds-turner

Description

@ds-turner

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  

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions