Package: labelled


Function: set_value_labels()


1. Add value labels for one or more variables (Var2 and Var3)

Review the data (d3)

# A tibble: 3 x 3
   Var1  Var2  Var3
  <dbl> <dbl> <dbl>
1     1     1     2
2     2     3     3
3     3     3     1

Add value labels to Var2 and Var3

  • Note: Parentheses are not required around labels here but I am using them around multi-word labels with spaces. Also using them around all labels will not hurt anything.
d3 <- d3 %>%
  labelled::set_value_labels(
    Var2 = c("not interested" = 1, "mildly interested" = 2, "very interested" = 3),
    Var3 = c(none = 1, "reduced-price" = 2, free = 3))

labelled::val_labels(d3)
$Var1
NULL

$Var2
   not interested mildly interested   very interested 
                1                 2                 3 

$Var3
         none reduced-price          free 
            1             2             3 

2. Create labels, then add labels to variables (Var2 and Var3)

Review the data (d3)

# A tibble: 3 x 3
   Var1  Var2  Var3
  <dbl> <dbl> <dbl>
1     1     1     2
2     2     3     3
3     3     3     1

Create labels

  • Note: Parentheses are not required around labels here, although adding them will not hurt.
lab <- c(disagree = 1, neutral = 2, agree = 3)

Add value labels to Var2 and Var3

d3 <- d3 %>%
  labelled::set_value_labels(Var2 = lab, Var3 = lab)

d3 %>%
  labelled::val_labels()
$Var1
NULL

$Var2
disagree  neutral    agree 
       1        2        3 

$Var3
disagree  neutral    agree 
       1        2        3 

Function: add_value_labels()


1. Update or add one or more value labels to a variable (Var3) without replacing/removing the other value labels

Review the data (d3)

# A tibble: 3 x 3
   Var1  Var2  Var3
  <dbl> <dbl> <dbl>
1     1     1     2
2     2     3     3
3     3     3     1

View current labels for Var3

$Var1
NULL

$Var2
NULL

$Var3
         none reduced-price          free 
            1             2             3 

Update the label for 1 in Var3

  • Note: Parentheses are not required around labels here, although adding them will not hurt.
d3 <- d3 %>%
  labelled::add_value_labels(Var3 = c(paid = 1))

d3 %>%
  labelled::val_labels()
$Var1
NULL

$Var2
NULL

$Var3
         paid reduced-price          free 
            1             2             3 

2. Add a new value label to a newly recoded variable (Var3) without replacing/removing the other value labels

Review the data (d3)

# A tibble: 3 x 3
   Var1  Var2  Var3
  <dbl> <dbl> <dbl>
1     1     1     2
2     2     3     3
3     3     3     1

View current labels for Var3

$Var1
NULL

$Var2
NULL

$Var3
 some a lot  none 
    1     2     3 

Recode 1 to 0 for Var3

  • Note: We used dplyr::recode() to recode 3 to 0. We use janitor::tabyl() to check the results of the recode.
d3 <- d3 %>%
  dplyr::mutate(Var3 = dplyr::recode(Var3, `3` = 0))

d3 %>%
  janitor::tabyl(Var3)
 Var3 n   percent
    0 1 0.3333333
    1 1 0.3333333
    2 1 0.3333333

Review the current labels after our recode

$Var1
NULL

$Var2
NULL

$Var3
 some a lot  none 
    1     2     3 

We no longer have any cases with a value of 3 since they have been recoded to 0. So our label for 3 is no longer necessary. Also our newly recoded value of 0 has no label.

  • Note: Before adding our new value label for 0, we remove the value label for 3 using labelled::remove_value_labels()
d3 <- d3 %>%
  labelled::remove_value_labels(Var3 = 3) %>%
  labelled::add_value_labels(Var3 = c("none" = 0))

d3 %>% 
  labelled::val_labels()
$Var1
NULL

$Var2
NULL

$Var3
 some a lot  none 
    1     2     0 

Function: labelled()


1. Add the same value labels to multiple variables with similar names (Var1, Var2, and Var3)

Review the data (d3)

# A tibble: 3 x 3
   Var1  Var2  Var3
  <dbl> <dbl> <dbl>
1     1     1     2
2     2     3     3
3     3     3     1

Add the same value labels to multiple variables

  • Note: We are modifying existing variables using dplyr::mutate()

  • Note: We are using dplyr::across() to apply a transformation to select columns and adding the tidyselect selection helper starts_with() to select variables that meet our criteria

  • Note: Other tidyselect selection helpers include ends_with(), contains(), matches(), and num_range(), and more.

  • Note: Parentheses are not required around labels here, although adding them will not hurt.

d3 <- d3 %>%
  dplyr::mutate(across(
    tidyselect::starts_with("Var"),
    ~ labelled::labelled(., labels = c(disagree = 1, neutral = 2, agree = 3))))

d3 %>% 
  labelled::val_labels()
$Var1
disagree  neutral    agree 
       1        2        3 

$Var2
disagree  neutral    agree 
       1        2        3 

$Var3
disagree  neutral    agree 
       1        2        3 

2. Add the same value labels to all variables

Review the data (d3)

# A tibble: 3 x 3
   Var1  Var2  Var3
  <dbl> <dbl> <dbl>
1     1     1     2
2     2     3     3
3     3     3     1

Add the same value labels to all variables

  • Note: We are modifying existing variables using dplyr::mutate()

  • Note: We are using dplyr::across() to apply a transformation to select columns and adding the tidyselect selection helper everything() to select all variables

  • Note: Other tidyselect selection helpers include ends_with(), contains(), matches(), and num_range(), and more.

  • Note: Parentheses are not required around labels here, although adding them will not hurt.

d3 <- d3 %>%
  dplyr::mutate(dplyr::across(
    tidyselect::everything(),
    ~ labelled::labelled(., labels = c(disagree = 1, neutral = 2, agree = 3))))

d3 %>%
  labelled::val_labels()
$Var1
disagree  neutral    agree 
       1        2        3 

$Var2
disagree  neutral    agree 
       1        2        3 

$Var3
disagree  neutral    agree 
       1        2        3 

Return to Label Data