Package: labelled


Function: to_factor()


1. Convert labelled numeric variables (Var2 and Var3) to a factor variable (exchange the numeric values for the 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

Add labels

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)
  )

Convert to factor

d3 %>% 
  labelled::to_factor()
# A tibble: 3 x 3
   Var1 Var2            Var3         
  <dbl> <fct>           <fct>        
1     1 not interested  reduced-price
2     2 very interested free         
3     3 very interested none         

2. Convert all labelled numeric variables to a factor variable (exchange the numeric values for the labels) and if one of your values is not labelled, turn that value to NA

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 only some values in Var3 (as you can see we skipped 2)

d3 <- d3 %>%
  labelled::set_value_labels(
    Var2 = c("not interested" = 1, "mildly interested" = 2, "very interested" = 3),
    Var3 = c("none" = 1, "free" = 3)
  )

Convert to factor by adding the argument nolabel_to_na=TRUE to turn unlabelled values to NA.

  • Note: If you do not add this argument and you leave it as the default nolabel_to_na=FALSE, unlabelled values will return as their numeric values rather than NA.
d3 %>%
  labelled::to_factor(nolabel_to_na = TRUE)
# A tibble: 3 x 3
   Var1 Var2            Var3 
  <dbl> <fct>           <fct>
1     1 not interested  <NA> 
2     2 very interested free 
3     3 very interested none 

Package: rio


Function: factorize()


1. Convert labelled numeric variables (Var2 and Var3) to factor variables (exchange the numeric values for the 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

Add labels

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)
  )

Convert to factor

d3 %>%
  rio::factorize()
# A tibble: 3 x 3
   Var1 Var2            Var3         
  <dbl> <fct>           <fct>        
1     1 not interested  reduced-price
2     2 very interested free         
3     3 very interested none         

Function: characterize()


1. Convert labelled numeric variables (Var2 and Var3) to character variables (exchange the numeric values for the 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

Add labels

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)
  )

Convert to character

d3 %>%
  rio::characterize()
# A tibble: 3 x 3
   Var1 Var2            Var3         
  <dbl> <chr>           <chr>        
1     1 not interested  reduced-price
2     2 very interested free         
3     3 very interested none         

Package: haven


Function: as_factor()


1. Convert labelled numeric variables (Var2 and Var3) to factor variables (exchange the numeric values for the 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

Add labels

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)
  )

Convert Var2 and Var3 to factor.

  • Note: The default for haven::as_factor() is to assign the labels in place of the number for any labelled value. However, using the levels argument, you can choose other options such as pasting together the label and value using the argument levels = “both”. You can see all arguments by typing ?as_factor in the console and choosing the haven package.
d3 %>%
  haven::as_factor()
# A tibble: 3 x 3
   Var1 Var2            Var3         
  <dbl> <fct>           <fct>        
1     1 not interested  reduced-price
2     2 very interested free         
3     3 very interested none         

You can also specifically convert only labelled variables (haven::is.labelled()) in your data frame (even though haven::as_factor will only convert variables with labels anyway) using the functions dplyr::mutate() in conjunction with dplyr::across() to edit selected variables.

d3 %>%
  dplyr::mutate(across(where(haven::is.labelled), haven::as_factor))
# A tibble: 3 x 3
   Var1 Var2            Var3         
  <dbl> <fct>           <fct>        
1     1 not interested  reduced-price
2     2 very interested free         
3     3 very interested none         

2. Select a specific labelled numeric variable (Var2) to convert to a factor variable (exchange the numeric values for the 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

Add labels

d3 <- d3 %>%
  labelled::set_value_labels(
    Var2 = c("not interested" = 1, "mildly interested" = 2, "very interested" = 3)
  )

Convert Var2 to a factor by including the dplyr::mutate() function to create a new variable (which replaces the old variable because we named it the same).

d3 %>%
  dplyr::mutate(Var2 = haven::as_factor(Var2))
# A tibble: 3 x 3
   Var1 Var2             Var3
  <dbl> <fct>           <dbl>
1     1 not interested      2
2     2 very interested     3
3     3 very interested     1

Return to Label Data