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

add `ordered` option to `as_label()`

januz opened this issue · comments

Thanks for all your work on sjlabelled. Of all the options to working with labelled data in R, I like your approach the most as it enables me to keep the labels no matter what I do with the data in between.

Depending on what I am doing at the moment, I typically like to switch in between the numeric/value representation as an numeric vector and a factor vector with the labels as factor levels. I can do this using

 vector_factor <- sjlabelled::as_label(vector_numeric, keep.labels  = TRUE)
 vector_numeric <- sjlabelled::as_numeric(vector_factor, use.labels = TRUE)

But when I need the factor to be ordered, I have to use ordered() on the factor vector and lose the labels thus cannot convert back to numeric. Would you consider adding a ordered option to as_label() that would return an ordered factor with pertaining the labels and being easily converted back to numeric using as_numeric().

Thanks so much!

(Edit: Realized that the other functionality I was asking for is already possible with the argument prefix. Sorry for not reading the documentation more thoroughly!)

Do you have a small reproducible example? Including the expected output?

Hi Daniel, Thanks a lot for getting back to me! While writing up the example, I found a solution to my original problem (going back from an ordered factor vector to a numeric one without losing labels). I found two additional problems though:

  • when reordering the factor levels (e.g., using forcats::fct_relevel(), converting back to numeric does not return the original vector and labels but changes both the numeric values and labels.
  • when using the prefix option of as_label(), converting back to numeric does not return the original vector but results in labels that include the prefix

My expectation/hope was that converting back to a numeric vector would always result in the original vector, independent of what I do with the factor levels in between. I thought that the label-value combination assigned as a label/attribute using sjlabelled would serve as a lookup table for how to convert back from the factor representation.

Here the example. Let me know if you need more information!

library(tidyverse)

x <- sample(1:4, 20, replace = TRUE) %>% 
  set_labels(
    labels = c("first" = 4, "second" = 1, "third" = 3, "fourth" = 2)
  )

# factor -----------------------------------------------------------------------

# create factor while keeping labels
x_factor <- x %>% 
  sjlabelled::as_label(keep.labels  = TRUE)

# convert back to numeric while keeping labels
x_factor %>% 
  sjlabelled::as_numeric()
  sjlabelled::as_numeric(use.labels = TRUE)

# works in both directions
x_factor %>% 
  sjlabelled::as_numeric(use.labels = TRUE) %>% 
  sjlabelled::as_label(keep.labels  = TRUE)

# factor (reordered levels) ----------------------------------------------------

# re-order factor levels
x_factor_relevel <- x_factor %>% 
  fct_relevel(c("first", "second", "third", "fourth"))

# convert to numeric
# Problems:
# - wrong numeric vector returned
# - labels are altered
x_factor_relevel %>% 
  sjlabelled::as_numeric(use.labels = TRUE)

# factor (ordered) -------------------------------------------------------------

# add ordered attribute to factor
x_factor_ordered <- x_factor %>% 
  ordered()

# convert to numeric
# Problems:
# - Error: number of levels differ
x_factor_ordered %>% 
  sjlabelled::as_numeric(use.labels = TRUE)

# convert to numeric --> works (which surprised me)
x_factor_ordered %>% 
  sjlabelled::as_numeric()

# factor (prefixed) ------------------------------------------------------------

# create prefixed factor
x_factor_prefixed <- x %>% 
  sjlabelled::as_label(prefix = TRUE, keep.labels  = TRUE)

# convert to numeric
# Problems:
# - Error: number of levels differ
x_factor_prefixed %>% 
  sjlabelled::as_numeric(use.labels = TRUE)

# convert to numeric
# Problems:
# - Assigns prefixed instead of original labels
x_factor_prefixed %>% 
  sjlabelled::as_numeric()

@strengejacke I see that this issue still has the "awaiting response" tag. Was my response not sufficient? Please let me know if you need more information. Thanks!

Sorry, I didn't get back to this issue yet. I'm looking into it, I think your reprex is sufficient.

@strengejacke, is there an update on this issue?