2

I am struggling to find a cleaner way to recode (numeric to numeric) multiple variables at once in a leaner way. Is there an alternative an easier way than this piece of code below?


mhomes_min <- mhomes %>% 
  mutate(contrib_private_3rd_party = recode(contrib_private_3rd_party,
     `1` = 1,
     `2` = 49,
     `3` = 100,
     `4` = 200,
     `5` = 500,
     `6` = 1000,
     `7` = 5000,
     `8` = 10000,
     `9` = 20000)) %>% 
  mutate(contrib_firm_3rd_party = recode(contrib_firm_3rd_party,
     `1` = 1,
     `2` = 49,
     `3` = 100,
     `4` = 200,
     `5` = 500,
     `6` = 1000,
     `7` = 5000,
     `8` = 10000,
     `9` = 20000)) %>% ...

1 Answers1

1

We can use mutate with across, specify the column names to be recoded in across to modify those columns

mhomes <- mhomes %>%
  mutate(across(c(contrib_private_3rd_party, contrib_firm_3rd_party), 
     ~ recode(., `1` = 1,
 `2` = 49,
 `3` = 100,
 `4` = 200,
 `5` = 500,
 `6` = 1000,
 `7` = 5000,
 `8` = 10000,
 `9` = 20000)))
akrun
  • 874,273
  • 37
  • 540
  • 662