rtidyverser-havenr-labelled

Create column with labels from labelled data


I have a data set with labelled data and would like to create a new column containing only the label as character.

Consider the following example:

value_labels <- tibble(value = 1:6, labels = paste0("value", 1:6))
df_data <- tibble(id = 1:10, var = floor(runif(10, 1, 6)))
df_data <- df_data %>% mutate(var = haven::labelled(var, labels = deframe(value_labels[2:1])))

This yields:

# A tibble: 10 x 2
      id        var
   <int>  <dbl+lbl>
 1     1 2 [value2]
 2     2 2 [value2]
 3     3 4 [value4]
 4     4 2 [value2]
 5     5 4 [value4]
 6     6 3 [value3]
 7     7 5 [value5]
 8     8 4 [value4]
 9     9 3 [value3]
10    10 1 [value1]

I would now like to create an additional column labs containing only the labels (i.e. value2 in rows 1 & 2, value4 in row 3 etc.

I tried using val_labs() (df_data %>% mutate(labs = val_labels(df_data$var, var))) unsuccessfully. Can someone point out the right way to do this?


Solution

  • haven::as_factor() is used for this. See the examples of the help page for labelled vectors.

    df_data %>%
      mutate(labs = as_factor(var))
    
    # A tibble: 10 × 3
          id        var  labs 
       <int>  <dbl+lbl> <fct> 
     1     1 2 [value2] value2
     2     2 5 [value5] value5
     3     3 2 [value2] value2
     4     4 5 [value5] value5
     5     5 2 [value2] value2
     6     6 4 [value4] value4
     7     7 5 [value5] value5
     8     8 4 [value4] value4
     9     9 5 [value5] value5
    10    10 3 [value3] value3