Package: dplyr


Function: recode()


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


1. Recode values using a long formatted data dictionary

Review the data (d23)

# A tibble: 4 x 3
  org_id org_name   city       
   <dbl> <chr>      <chr>      
1    123 Org Name C London     
2    124 Org Name D New York   
3    125 Org Name F London     
4    127 Org Name A Los Angeles

Review our current long formatted data dictionary (dict_long) that contains the old values of our variable org_name (old_values) and the new values that we want instead (new_values).

Note that this dataset is not in the usual data dictionary format I would think of (with each row corresponding to one variable). In this case, this would be a specialized long format type of codebook that you may have received or created solely for the purpose of recoding values.

# A tibble: 6 x 2
  old_value  new_value     
  <chr>      <chr>         
1 Org Name A New Org Name A
2 Org Name B <NA>          
3 Org Name C New Org Name C
4 Org Name D <NA>          
5 Org Name E New Org Name E
6 Org Name F <NA>          

In order to use this information to recode our values, we first need to pull a named vector that includes any value that is recoded.

dict_long <- dict_long %>%
  dplyr::filter(!is.na(new_value)) %>%
  tibble::deframe()

We can now use the named vector to recode our values. We can use !!! to splice a our vector into the argument of a quoting expression.

d23 %>% 
  dplyr::mutate(org_name = dplyr::recode(org_name, !!!dict_long))
# A tibble: 4 x 3
  org_id org_name       city       
   <dbl> <chr>          <chr>      
1    123 New Org Name C London     
2    124 Org Name D     New York   
3    125 Org Name F     London     
4    127 New Org Name A Los Angeles

Go to Recode using wide formatted data dictionary

Return to Recode