Q: Labels to values in R I have a data frame where all columns have labels. E.g.,
unique(df$var1)
...
Labels:
 value            label
     1 21 November 2020
     2 22 November 2020
     3 23 November 2020
     4 24 November 2020

I would like to work with the labels as I would work with the values. Is there a way to replace the values of a column with its labels?
An minimal example of the data can be created like this:
library(labelled)
df <- data.frame(
  var1 <- labelled(seq(1, 4, 1), c("21 November 2020" = 1, 
                                   "22 November 2020" = 2, 
                                   "23 November 2020" = 3,
                                   "24 November 2020" = 4)))

The desired output would be:
unique(df$var1)
[1] 21 November 2020
[2] 22 November 2020
[3] 23 November 2020
[4] 24 November 2020

The head() of the variable is:
<labelled<double>[4]>
[1] 1 2 3 4

Labels:
 value            label
     1 21 November 2020
     2 22 November 2020
     3 23 November 2020
     4 24 November 2020


A: Here would be my solution using the packages labelled and dplyr.
Let's first create some dummy data:
df <- data.frame(
  var = 1:10,
  var2 = labelled(seq(1, 50, 5), c("banana" = 1, "orange" = 6, "apple" = 36)))

If you look at the column containing labelled information, this is what you get:
> df$var2
<labelled<double>[10]>
 [1]  1  6 11 16 21 26 31 36 41 46

Labels:
 value  label
     1 banana
     6 orange
    36  apple

This is how you would add the labels to an own seperate column using a left join:
df_join <- data.frame("values" = as.numeric(val_labels(df$var2)),
                      "labels" = names(val_labels(df$var2)))
df <- df %>%
  left_join(df_join, by = c("var2" = "values"))

And this is the result:
> df
   var var2 labels
1    1    1 banana
2    2    6 orange
3    3   11   <NA>
4    4   16   <NA>
5    5   21   <NA>
6    6   26   <NA>
7    7   31   <NA>
8    8   36  apple
9    9   41   <NA>
10  10   46   <NA>

