Learn R Programming

sjmisc (version 1.8)

rec: Recode variables

Description

Recodes the categories / values of a variable x into new category values.

Usage

rec(x, recodes, as.fac = FALSE, var.label = NULL, val.labels = NULL)
rec(x, as.fac = FALSE, var.label = NULL, val.labels = NULL) <- value

Arguments

x
Numeric, charactor or factor variable that should be recoded; or a data.frame or list of variables.
recodes
String with recode pairs of old and new values. See 'Details' for examples.
as.fac
Logical, if TRUE, recoded variable is returned as factor. Default is FALSE, thus a numeric variable is returned.
var.label
Optional string, to set variable label attribute for the recoded variable (see set_label). If NULL (default), variable label attribute of x will be used (if present).
val.labels
Optional character vector, to set value label attributes of recoded variable (see set_labels). If NULL (default), no value labels will be set.
value
See recodes.

Value

A numeric variable (or a factor, if as.fac = TRUE or if x was a character vector) with recoded category values, or a data frame or list-object with recoded categories for all variables.

Details

The recodes string has following syntax:
recode pairs
each recode pair has to be separated by a ;, e.g. recodes = "1=1; 2=4; 3=2; 4=3"

multiple values
multiple old values that should be recoded into a new single value may be separated with comma, e.g. "1,2=1; 3,4=2"

value range
a value range is indicated by a colon, e.g. "1:4=1; 5:8=2" (recodes all values from 1 to 4 into 1, and from 5 to 8 into 2)

"min" and "max"
minimum and maximum values are indicates by min (or lo) and max (or hi), e.g. "min:4=1; 5:max=2" (recodes all values from minimum values of x to 4 into 1, and from 5 to maximum values of x into 2)

"else"
all other values except specified are indicated by else, e.g. "3=1; 1=2; else=3" (recodes 3 into 1, 1 into 2 and all other values into 3)

"copy"
the "else"-token can be combined with copy, indicating that all remaining, not yet recoded values should stay the same (are copied from the original value), e.g. "3=1; 1=2; else=copy" (recodes 3 into 1, 1 into 2 and all other values like 2, 4 or 5 etc. will not be recoded, but copied, see 'Examples')

NA's
NA values are allowed both as old and new value, e.g. "NA=1; 3:5=NA" (recodes all NA from old value into 1, and all old values from 3 to 5 into NA in the new variable)

"rev"
"rev" is a special token that reverses the value order (see 'Examples')

See Also

set_na for setting NA values, replace_na to replace NA's with specific value, recode_to for re-shifting value ranges and ref_lvl to change the reference level of (numeric) factors.

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)

# or:
# rec(efc$e42dep) <- "1,2=1; 3,4=2"
# table(efc$e42dep, exclude = NULL)

# keep value labels. variable label is automatically preserved
str(rec(efc$e42dep, "1,2=1; 3,4=2",
        val.labels = c("low dependency", "high dependency")))

# 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)

# recode only selected values, copy remaining
table(efc$e15relat)
table(rec(efc$e15relat, "1,2,4=1; else=copy"))

# recode variables with same categorie in a data frame
head(efc[, 6:9])
head(rec(efc[, 6:9], "1=10;2=20;3=30;4=40"))

# recode list of variables. create dummy-list of
# variables with same value-range
dummy <- list(efc$c82cop1, efc$c83cop2, efc$c84cop3)
# show original distribution
lapply(dummy, table, exclude = NULL)
# show recodes
lapply(rec(dummy, "1,2=1; NA=9; else=copy"), table, exclude = NULL)


# recode character vector
dummy <- c("M", "F", "F", "X")
rec(dummy, "M=Male; F=Female; X=Refused")


# recode non-numeric factors
data(iris)
rec(iris$Species, "setosa=huhu; else=copy")

Run the code above in your browser using DataLab