Skip to content

How lenient should labelled be when combining different labels that map to the same value? #667

@manhnguyen48

Description

@manhnguyen48

It seems pivot_longer would lose the value labels (produced by labelled). This can be avoided if transformed to factors first. Is this expected behaviour? It'd be nice to at least have some warnings.


Brief description of the problem

library(labelled)
library(tidyverse)
example_data <- tibble::tibble(serial = 1:100, 
                       age = sample(1:6, size = 100, replace = T) |>
                         labelled::set_variable_labels(.labels = "Age") |> 
                         labelled::set_value_labels(.labels = c("18-24"=1, "25-34"=2,"35-44"=3,
                                                                "45-54"=4,"55-64"=5,"65+"=6)), 
                       gender = sample(1:2, size = 100, replace=T) |> 
                         labelled::set_variable_labels(.labels = "Gender") |> 
                         labelled::set_value_labels(.labels = c("Male" = 1, "Female"=2)))

#Value labels for Gender is lost
tidyr::pivot_longer(example_data, c(age,gender)) |> 
  dplyr::count(name,value)
#> # A tibble: 8 x 3
#>   name       value     n
#>   <chr>  <int+lbl> <int>
#> 1 age    1 [18-24]    16
#> 2 age    2 [25-34]    17
#> 3 age    3 [35-44]    16
#> 4 age    4 [45-54]    14
#> 5 age    5 [55-64]    18
#> 6 age    6 [65+]      19
#> 7 gender 1 [18-24]    48
#> 8 gender 2 [25-34]    52
#It works if you convert to factors
tidyr::pivot_longer(example_data, c(age,gender), values_transform = forcats::as_factor) |> 
  dplyr::count(name,value)
#> # A tibble: 8 x 3
#>   name   value      n
#>   <chr>  <fct>  <int>
#> 1 age    18-24     16
#> 2 age    25-34     17
#> 3 age    35-44     16
#> 4 age    45-54     14
#> 5 age    55-64     18
#> 6 age    65+       19
#> 7 gender Male      48
#> 8 gender Female    52

Created on 2022-03-19 by the reprex package (v2.0.1)

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