Learn R Programming

rosetta (version 0.3.8)

recode: Recode a Variable (car version)

Description

This function is from the car package. Please see that help page for details: car::recode().

Usage

recode(var, recodes, as.factor, as.numeric = TRUE, levels)

Arguments

var

numeric vector, character vector, or factor.

recodes

character string of recode specifications: see below.

as.factor

return a factor; default is TRUE if var is a factor, FALSE otherwise.

as.numeric

if TRUE (the default), and as.factor is FALSE, then the result will be coerced to numeric if all values in the result are numerals<U+2014>i.e., represent numbers.

levels

an optional argument specifying the order of the levels in the returned factor; the default is to use the sort order of the level names.

References

Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.

Examples

Run this code
# NOT RUN {
x<-rep(1:3,3)
x
rosetta::recode(
  x,
  "c(1,2)='A'; else='B'"
);
rosetta::recode(
  x,
  "1:2='A'; 3='B'"
);
# }

Run the code above in your browser using DataLab