'How to recode range of columns in R

I am trying to recode the 5th-18th variable in a data frame of 173 variables. I am new to R so perhaps am missing something simple, but I have tried nearly every fix I can find provided in similar questions on here. Nothing works. Here is one approach I've recently tried.

Raw_w %>% 
      mutate_at(vars(5:18), 
                ~as.numeric(recode(.,
                                   "Strongly Disagree" = 1,
                                               "Disagree" = 2,
                                               "Somewhat Disagree" = 3,
                                               "Somewhat Agree" = 4,
                                               "Agree"= 5, 
                                               "Strongly Agree" = 6)))

To which I get this error:

Error: indexes are greater then number of items: 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18

The other approach I tried was just to go through a list of variable names. Like so(this is an abbreviated var list fyi ) :

Raw_w %>% 
    mutate_at(c("bsl_pol2", "bsl_pol3", "bsl_pol4"), 
              ~as.numeric(recode(.,
                                 "Strongly Disagree" = 1,
                                 "Disagree" = 2,
                                 "Somewhat Disagree" = 3,
                                 "Somewhat Agree" = 4,
                                 "Agree"= 5, 
                                 "Strongly Agree" = 6)))

But then I get this error: Error: Problem with `mutate()` column `bsl_pol2`. ℹ `bsl_pol2 = (structure(function (..., .x = ..1, .y = ..2, . = ..1) ...`. x 'recode': all recodings should be formula but: 1

I know that these topics are covered elsewhere, but trust that all my many hours of trouble shooting this issue have borrowed from those threads and nothing seems to work. Please help me not to retreat back to STATA!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source