Skip to content

Instantly share code, notes, and snippets.

@DavisVaughan
Created February 20, 2026 13:33
Show Gist options
  • Select an option

  • Save DavisVaughan/f132a9269c77a6f256090bb761726e5e to your computer and use it in GitHub Desktop.

Select an option

Save DavisVaughan/f132a9269c77a6f256090bb761726e5e to your computer and use it in GitHub Desktop.
recode-values
library(dplyr)
likert <- tibble(
score = c(1, 2, 3, 4, 5, 2, 3, 1, 4)
)
# Typical `case_when()` solution
likert |>
mutate(
category = case_when(
score == 1 ~ "Strongly disagree",
score == 2 ~ "Disagree",
score == 3 ~ "Neutral",
score == 4 ~ "Agree",
score == 5 ~ "Strongly agree"
)
)
# `recode_values()` removes a lot of repetition and lets you focus on the mapping
likert |>
mutate(
category = score |>
recode_values(
1 ~ "Strongly disagree",
2 ~ "Disagree",
3 ~ "Neutral",
4 ~ "Agree",
5 ~ "Strongly agree"
)
)
# Often you don't really want to inline the
# lookup table in the `recode_values()` call
# lookup <- readr::read_csv("lookup.csv")
lookup <- tribble(
~from , ~to ,
1 , "Strongly disagree" ,
2 , "Disagree" ,
3 , "Neutral" ,
4 , "Agree" ,
5 , "Strongly agree" ,
)
likert |>
mutate(category = recode_values(score, from = lookup$from, to = lookup$to))
# For added safety, use `unmatched = "error"` if you think you've covered all
# the cases!
likert |>
mutate(
category = score |>
recode_values(from = lookup$from, to = lookup$to, unmatched = "error")
)
likert <- likert |> add_row(score = 0)
likert |>
mutate(
category = score |>
recode_values(from = lookup$from, to = lookup$to, unmatched = "error")
)
likert[10, ]
@DavisVaughan
Copy link
Author

Rendered

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

likert <- tibble(
  score = c(1, 2, 3, 4, 5, 2, 3, 1, 4)
)

# Typical `case_when()` solution
likert |>
  mutate(
    category = case_when(
      score == 1 ~ "Strongly disagree",
      score == 2 ~ "Disagree",
      score == 3 ~ "Neutral",
      score == 4 ~ "Agree",
      score == 5 ~ "Strongly agree"
    )
  )
#> # A tibble: 9 × 2
#>   score category         
#>   <dbl> <chr>            
#> 1     1 Strongly disagree
#> 2     2 Disagree         
#> 3     3 Neutral          
#> 4     4 Agree            
#> 5     5 Strongly agree   
#> 6     2 Disagree         
#> 7     3 Neutral          
#> 8     1 Strongly disagree
#> 9     4 Agree

# `recode_values()` removes a lot of repetition and lets you focus on the mapping
likert |>
  mutate(
    category = score |>
      recode_values(
        1 ~ "Strongly disagree",
        2 ~ "Disagree",
        3 ~ "Neutral",
        4 ~ "Agree",
        5 ~ "Strongly agree"
      )
  )
#> # A tibble: 9 × 2
#>   score category         
#>   <dbl> <chr>            
#> 1     1 Strongly disagree
#> 2     2 Disagree         
#> 3     3 Neutral          
#> 4     4 Agree            
#> 5     5 Strongly agree   
#> 6     2 Disagree         
#> 7     3 Neutral          
#> 8     1 Strongly disagree
#> 9     4 Agree

# Often you don't really want to inline the
# lookup table in the `recode_values()` call

# lookup <- readr::read_csv("lookup.csv")
lookup <- tribble(
  ~from , ~to                 ,
      1 , "Strongly disagree" ,
      2 , "Disagree"          ,
      3 , "Neutral"           ,
      4 , "Agree"             ,
      5 , "Strongly agree"    ,
)

likert |>
  mutate(category = recode_values(score, from = lookup$from, to = lookup$to))
#> # A tibble: 9 × 2
#>   score category         
#>   <dbl> <chr>            
#> 1     1 Strongly disagree
#> 2     2 Disagree         
#> 3     3 Neutral          
#> 4     4 Agree            
#> 5     5 Strongly agree   
#> 6     2 Disagree         
#> 7     3 Neutral          
#> 8     1 Strongly disagree
#> 9     4 Agree

# For added safety, use `unmatched = "error"` if you think you've covered all
# the cases!

likert |>
  mutate(
    category = score |>
      recode_values(from = lookup$from, to = lookup$to, unmatched = "error")
  )
#> # A tibble: 9 × 2
#>   score category         
#>   <dbl> <chr>            
#> 1     1 Strongly disagree
#> 2     2 Disagree         
#> 3     3 Neutral          
#> 4     4 Agree            
#> 5     5 Strongly agree   
#> 6     2 Disagree         
#> 7     3 Neutral          
#> 8     1 Strongly disagree
#> 9     4 Agree

likert <- likert |> add_row(score = 0)

likert |>
  mutate(
    category = score |>
      recode_values(from = lookup$from, to = lookup$to, unmatched = "error")
  )
#> Error in `mutate()`:
#> ℹ In argument: `category = recode_values(score, from = lookup$from, to =
#>   lookup$to, unmatched = "error")`.
#> Caused by error in `recode_values()`:
#> ! Each location must be matched.
#> ✖ Location 10 is unmatched.

likert[10, ]
#> # A tibble: 1 × 1
#>   score
#>   <dbl>
#> 1     0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment