I have the following 2 lines of code that I would like to make into a one-liner (would like to do it with base R). Any help would be appreciated.
#recode the sex variable
ibr.sub$SEX[ibr.sub$SEX == "1" | ibr.sub$SEX == "3"] <- "-1"
ibr.sub$SEX[ibr.sub$SEX == "2" | ibr.sub$SEX == "4"] <- "1"
>Solution :
We may do
library(dplyr)
ibr.sub$SEX <- case_when(ibr.sub$SEX %in% c(1,3)~ "-1", ibr.sub$SEX %in% c(2,4) ~ "1", TRUE ~ ibr.sub$SEX)