0

I have a df that I want to switch the score in some of its columns when 5 will be 1, 4 will be 3, and so on... q_switch is a vector that contains the names of the columns in which I want to switch the score

> q_switch
 [1] "E1"  "E2"  "E3"  "E4"  "E5"  "E6"  "E7"  "E8"  "E9"...

I tried to so the following as I saw in some answers:

data%\>%
mutate_at(
.vars = vars(q_switch),
.funs = funs(. = case_when(
. == 5 ~ 1
. == 4 ~ 2
. == 2 ~ 4
. == 1 ~ 5
)))

But when I'm trying to run this code but an error message pop up and says:

Error: unexpected symbol in:
"          . == 5 ~ 1
."

What am I doing wrong?

Maya
  • 1
  • 1
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Is `%\>%` from some package? Also `mutate_at` has been superseded by `mutate`+`across` so it's probably better to use the new syntax. Also you definitely commas between your different `case_when` conditions and that's what's causing the syntax error – MrFlick Nov 17 '22 at 14:15
  • Thank you so much! the problem really was the commas – Maya Nov 17 '22 at 16:18

0 Answers0