Learn R Programming

sjmisc (version 2.3.0)

recode_to: Recode variable categories into new values

Description

Recodes (or "renumbers") the categories of variables into new category values, beginning with the lowest value specified by 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, suffix = "_r0")

Arguments

x
A vector or data frame.
...
Optional, unquoted names of variables. Required, if x is a data frame (and no vector) and only selected variables from x should be processed. You may also use functions like : or dplyr's select_helpers. The latter must be stated as formula (i.e. beginning with ~). See 'Examples' or package-vignette.
lowest
Indicating the lowest category value for recoding. Default is 0, so the new variable starts with value 0.
highest
If specified and greater than lowest, all category values larger than highest will be set to NA. Default is -1, i.e. this argument is ignored and no NA's will be produced.
suffix
String value, will be appended to variable (column) names of x, if x is a data frame. If x is not a data frame, this argument will be ignored. The default value to suffix column names in a data frame depends on the function call:
  • recoded variables (rec()) will be suffixed with "_r"
  • recoded variables (recode_to()) will be suffixed with "_r0"
  • dichotomized variables (dicho()) will be suffixed with "_d"
  • grouped variables (split_var()) will be suffixed with "_g"
  • grouped variables (group_var()) will be suffixed with "_gr"
  • standardized variables (std()) will be suffixed with "_z"
  • centered variables (center()) will be suffixed with "_c"

Value

x with recoded category values, where lowest indicates the lowest value; If x is a data frame, only the recoded variables will be returned.

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, lowest = 1)

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

# recode multiple variables at once
data(efc)
recode_to(efc, c82cop1, c83cop2, c84cop3)

library(dplyr)
efc %>%
  select(c82cop1, c83cop2, c84cop3) %>%
  mutate(
    c82new = recode_to(c83cop2, lowest = 5),
    c83new = recode_to(c84cop3, lowest = 3)
  ) %>%
  head()


Run the code above in your browser using DataLab