Learn R Programming

sjmisc (version 1.0.0)

rec: Recode numeric variables

Description

Recodes the categories of a (numeric) variable x into new category values.

Usage

rec(x, recodes)

Arguments

x
a numeric variable (vector) or a factor with numeric levels that should be recoded.
recodes
a string with recode pairs of old and new values. See notes for details on this parameter.

Value

  • A numeric variable with recoded category values.

See Also

set_na for setting NA values and recode_to for re-shifting value ranges.

Examples

Run this code
data(efc)
table(efc$e42dep, exclude = NULL)

# replace NA with 5
table(rec(efc$e42dep, "1=1;2=2;3=3;4=4;NA=5"), exclude = NULL)

# recode 1 to 2 into 1 and 3 to 4 into 2
table(rec(efc$e42dep, "1,2=1; 3,4=2"), exclude = NULL)

# recode 1 to 3 into 4 into 2
table(rec(efc$e42dep, "min:3=1; 4=2"), exclude = NULL)

# recode 2 to 1 and all others into 2
table(rec(efc$e42dep, "2=1; else=2"), exclude = NULL)

# reverse value order
table(rec(efc$e42dep, "rev"), exclude = NULL)

Run the code above in your browser using DataLab