Learn R Programming

sjmisc (version 1.0.2)

recode_to: Recode variable categories into new values.

Description

Recodes (or "renumbers") the categories of var into new category values, beginning with the lowest value specified by parameter lowest. Useful if you want to recode dummy variables with 1/2 coding to 0/1 coding, or recoding scales from 1-4 to 0-3 etc.

Usage

recode_to(x, lowest = 0, highest = -1)

Arguments

x
A variable (vector) or a data frame that should be recoded.
lowest
Indicating the lowest category value after recoding. Default is 0, so the new variable starts with the category value 0.
highest
If specified and larger than lowest, all category values larger than highest will be set to NA. Default is -1, i.e. this parameter is ignored and no NA's will be produced.

Value

  • A new variable with recoded category values, where lowest indicates the lowest value; or a data frame where variables have been recoded as described.

See Also

rec for general recoding of variables and set_na for setting NA values.

Examples

Run this code
# recode 1-4 to 0-3
dummy <- sample(1:4, 10, replace = TRUE)
recode_to(dummy)

# recode 3-6 to 0-3
# note that numeric type is returned
dummy <- as.factor(3:6)
recode_to(dummy)

# lowest value starting with 1
dummy <- sample(11:15, 10, replace = TRUE)
recode_to(dummy, 1)

# lowest value starting with 1, highest with 3
# all others set to NA
dummy <- sample(11:15, 10, replace = TRUE)
recode_to(dummy, 1, 3)

Run the code above in your browser using DataLab