How to use recode() in a loop?

24 Views Asked by At

I'm currently working with a big dataframe and I need to recode the values of some columns (0, 1 or 8 to N, Y, U). For one column, I can use:

df %>% mutate(colname = recode(colname, "0" = "N", "1" = "Y", "8" = "U")).

This works fine, but to avoid repetition in my code (as this has to be done for several columns), I would like to put this code into a loop and that's where I get stuck...

I can't find a way to put the colnames into a vector so it functions within a loop.

if the colnames are a character, it won't work, if I use noquote() or asname() I get the error that recode() won't work with the classes noquote or asname, if I use df[1], I'm working with the data from the column, instead of with the colname... Anyone that can help me?

0

There are 0 best solutions below