Based on car::recode()
.
RECODE(var, recodes)
Variable (numeric, character, or factor).
Character string: e.g., "lo:1=0; c(2,3)=1; 4=2; 5:hi=3; else=999"
.
A vector of recoded variable.
# NOT RUN {
d=data.table(var=c(NA, 0, 1, 2, 3, 4, 5, 6))
d
d[,":="(var.recoded=RECODE(var, "lo:1=0; c(2,3)=1; 4=2; 5:hi=3; else=999"))]
d
# }
Run the code above in your browser using DataLab