I would recommend using recode(). I think that a re-usable function for a varied number of questions, answers and numerical values is likely going to be basically just a few less lines of code, more prone to bugs, etc.
library(tidyverse)
survey <- tribble(
~ id, ~ alcohol, ~ injured
, 1, "Never", "No"
, 2, "Monthly or less", "Yes but not in the last year"
, 3, "2-4 times a month", "Yes"
, 4, "2-3 times a week", "No"
)
survey %>%
mutate(
alcohol = recode(
alcohol,
"Never" = 0,
"Monthly or less" = 1,
"2-4 times a month" = 2,
"2-3 times a week" = 3)
)
#> # A tibble: 4 x 3
#> id alcohol injured
#> <dbl> <dbl> <chr>
#> 1 1 0 No
#> 2 2 1 Yes but not in the last year
#> 3 3 2 Yes
#> 4 4 3 No
One thing you can do is to store the levels and their numerical equivalents in a named character vector and use the splice operator from rlang – !!! – to break the vector into named arguments for recode(), like this:
injured_levels <- c(
"No" = 0,
"Yes but not in the last year" = 2,
"Yes" = 5
)
survey %>%
mutate(injured = recode(injured, !!!injured_levels))
#> # A tibble: 4 x 3
#> id alcohol injured
#> <dbl> <chr> <dbl>
#> 1 1 Never 0
#> 2 2 Monthly or less 2
#> 3 3 2-4 times a month 5
#> 4 4 2-3 times a week 0
That's pretty compact, easily readable and easy to update and maintain.
Created on 2019-02-06 by the reprex package (v0.2.1)