Home > Software design >  case_when over multiple columns
case_when over multiple columns

Time:08-08

I have a DF with 4 coulmns and I'm trying to use case_when:

myDF %>%
  mutate(across(.cols = c(Coder_1, Coder_2, Coder_3),
                ~case_when(coderAvg == "1" ~ 1)))

Whenever there is the value 1 under coderAvg column,

it sets the value "1" under all the columns: Coder_1, Coder_2, Coder_3 and it works fine.

The problem is that whenever coderAvg has different value, it sets these columns (Coder_1, Coder_2, Coder_3) into NA.

I've tried to use

~case_when(coderAvg == "1" ~ 1,
               TRUE ~ ???)))

what what should I write insted of the ??? so that values remain the same?

CodePudding user response:

I believe you need this: updated

myDF %>%
  mutate(across(.cols = c(Coder_1, Coder_2, Coder_3),
                ~ifelse(coderAvg == "1",1, .)))
  • Related