I have a dataframe of survey responses on a scale 1-5. I want to recode them so that 1-3 is a 0, and 4-5 is a 1, preferably without just using a for loop. I understand that I should be able to use mutate_all, but I can't figure out how to build the .funs to make that work. Would lapply/sapply work instead?
Based on my understanding of mutate_all, I tried this
A03 <- A02 %>% mutate_all(if(%in%c(4,5){1}))
and got this error:
Error: unexpected SPECIAL in "A03 <- A02 %>% mutate_all(if(%in%"
Any insight would be very valuable.
ETA:
I managed to cobble together a working answer using
recode_response <- function(r){
if(r %in% c(4,5)){
1
} else(0)
}
A03 <- A02 %>% apply(c(1,2),recode_response)
but
A03 <- A02 %>% mutate(across(everything(), ~ifelse(.x <= 3, 0, 1)))
is a slicker answer.