Package: dplyr


Function: recode_factor()


Note: The dplyr::recode_factor() formula is: old value=new value, this is opposite of dplyr::rename()

Note: The function dplyr::recode_factor() will convert your variable to factor and recode it.


1. Convert a character variable (lunch) into a factor and recode it

Review the data (d4)

# A tibble: 3 x 3
  id    gender lunch
  <chr> <chr>  <chr>
1 a     m      f    
2 b     f      r    
3 c     n      p    

Recode lunch status

  • Note: We did not have to put quotes around the old values because dplyr::recode_factor() replaces character or factor values by their name.

  • Note: Quotes are required around the new value when recoding into a character/factor variable.

  • Note: We are recoding back into the same variable using dplyr::mutate(). However, we could have recoded into a new variable by changing the name of lunch.

d4 <- d4 %>%
  dplyr::mutate(lunch = dplyr::recode_factor(lunch, p = "paid", r = "reduced", f = "free"))

d4
# A tibble: 3 x 3
  id    gender lunch  
  <chr> <chr>  <fct>  
1 a     m      free   
2 b     f      reduced
3 c     n      paid   

Review the factor levels

levels(d4$lunch)
[1] "paid"    "reduced" "free"   

2. Recode a numeric variable (lunch) into a factor

Review the data (d7)

# A tibble: 3 x 3
  id    gender lunch
  <chr> <chr>  <dbl>
1 a     m          1
2 b     f          2
3 c     n          3

Recode lunch status

Note: We did have to put back ticks around the old values because dplyr::recode_factor() replaces numeric values by either their name or their position. If a number with no quotes/backticks is given, it will assume it is a position.

d7 <- d7 %>%
  dplyr::mutate(lunch = dplyr::recode_factor(
    lunch,
    "1" = "paid",
    "2" = "reduced",
    "3" = "free"
  ))

d7
# A tibble: 3 x 3
  id    gender lunch  
  <chr> <chr>  <fct>  
1 a     m      paid   
2 b     f      reduced
3 c     n      free   

Review the factor levels

levels(d7$lunch)
[1] "paid"    "reduced" "free"   


Package: forcats


Function: fct_recode()


Note: The forcats::fct_recode() formula is: new value = old value, this is opposite of dplyr::recode() but the same as dplyr::rename()

Note: The function forcats::fct_recode will convert your variable to factor and recode it.


1. Recode a numeric variable (lunch) into a factor

Review the data (d7)

# A tibble: 3 x 3
  id    gender lunch
  <chr> <chr>  <dbl>
1 a     m          1
2 b     f          2
3 c     n          3

Recode lunch status

  • Note: forcats::fct_recode will only recode factor or character variables. If your variable is numeric, you will need to convert it to character or factor before using recode factor. As you see below, I converted our numeric variable to character in order to use the function.

  • Note: Counterintuitive to dplyr::recode(), 1) you do have to put quotes around your character old value, 2) if you new values are character, you do not have to put quotes around them, 3) however, if your new values are numeric, you do need quotes around them.

d7 <- d7 %>%
  dplyr::mutate(lunch = forcats::fct_recode(
    as.character(lunch),
    paid = "1",
    reduced = "2",
    free = "3"
  ))

d7
# A tibble: 3 x 3
  id    gender lunch  
  <chr> <chr>  <fct>  
1 a     m      paid   
2 b     f      reduced
3 c     n      free   

Review the factor levels

base::levels(d7$lunch)
[1] "paid"    "reduced" "free"   

Return to Recode