Recoding mutilple columns in 1 shot

The function has a few issues:

  • It needs to return something at the end (probably x). See ?return.
  • We cannot write x$y programmatically passing y, unfortunately. Instead x[[y]] <- should work where y is a string column name with quotes.
  • if and else are for control flow (determining which pieces of code get run), while you you want this to apply in a vectorized way to each element of x$y.

I'd suggest approaching this with a lookup table and a left join. The tidyverse way is shown here:

This also looks related, using case_when as a substitute for if/else: