rec {misty} | R Documentation |
Recode Variable
Description
This function recodes numeric vectors, character vectors, or factors according to recode specifications.
Usage
rec(..., data = NULL, spec, as.factor = FALSE, levels = NULL, append = TRUE,
name = ".e", as.na = NULL, table = FALSE, check = TRUE)
Arguments
... |
a numeric vector, character vector, factor, matrix or data
frame. Alternatively, an expression indicating the variable
names in |
data |
a data frame when specifying one or more variables in the
argument |
spec |
a character string of recode specifications (see 'Details'). |
as.factor |
logical: if |
levels |
a character vector for specifying the levels in the returned factor. |
as.na |
a numeric vector indicating user-defined missing values,
i.e. these values are converted to |
append |
logical: if |
name |
a character string or character vector indicating the names
of the recoded variables. By default, variables are named with the ending
|
table |
logical: if |
check |
logical: if |
Details
Recode specifications appear in a character string, separated by semicolons
(see the examples below), of the form input = output. If an input value satisfies
more than one specification, then the first (from left to right) applies. If
no specification is satisfied, then the input value is carried over to the
result. NA
is allowed in input and output. Several recode specifications
are supported:
- Single Value
For example,
spec = "0 = NA".
- Vector of Values
For example,
spec = "c(7, 8, 9) = 'high'"
.- Range of Values
For example,
spec = "7:9 = 'C'"
. The special valueslo
(lowest value) andhi
(highest value) may appear in a range. For example,spec = "lo:10 = 1"
. Note that:
is not the R sequence operator. In addition you may not use:
with the collect operator, e.g.,spec = "c(1, 3, 5:7)"
will cause an error.- else
For example,
spec = "0 = 1; else = NA"
. Everything that does not fit a previous specification. Note thatelse
matches all otherwise unspecified values on input, includingNA
.
Value
Returns a numeric vector or data frame with the same length or same number of
rows as ...
containing the recoded coded variable(s).
Note
This function was adapted from the recode()
function in the car
package by John Fox and Sanford Weisberg (2019).
Author(s)
Takuya Yanagida takuya.yanagida@univie.ac.at
References
Fox, J., & Weisberg S. (2019). An R Companion to Applied Regression (3rd ed.). Thousand Oaks CA: Sage. URL: https://socialsciences.mcmaster.ca/jfox/Books/Companion/
See Also
Examples
#----------------------------------------------------------------------------
# Numeric vector
x.num <- c(1, 2, 4, 5, 6, 8, 12, 15, 19, 20)
# Example 1a: Recode 5 = 50 and 19 = 190
rec(x.num, spec = "5 = 50; 19 = 190")
# Example 1b: Recode 1, 2, and 5 = 100 and 4, 6, and 7 = 200 and else = 300
rec(x.num, spec = "c(1, 2, 5) = 100; c(4, 6, 7) = 200; else = 300")
# Example 1c: Recode lowest value to 10 = 100 and 11 to highest value = 200
rec(x.num, spec = "lo:10 = 100; 11:hi = 200")
# Example 1d: Recode 5 = 50 and 19 = 190 and check recoding
rec(x.num, spec = "5 = 50; 19 = 190", table = TRUE)
#----------------------------------------------------------------------------
# Character vector
x.chr <- c("a", "c", "f", "j", "k")
# Example 2a: Recode a to x
rec(x.chr, spec = "'a' = 'X'")
# Example 2b: Recode a and f to x, c and j to y, and else to z
rec(x.chr, spec = "c('a', 'f') = 'x'; c('c', 'j') = 'y'; else = 'z'")
# Example 2c: Recode a to x and coerce to a factor
rec(x.chr, spec = "'a' = 'X'", as.factor = TRUE)
#----------------------------------------------------------------------------
# Factor
x.fac <- factor(c("a", "b", "a", "c", "d", "d", "b", "b", "a"))
# Example 3a: Recode a to x, factor levels ordered alphabetically
rec(x.fac, spec = "'a' = 'x'")
# Example 3b: Recode a to x, user-defined factor levels
rec(x.fac, spec = "'a' = 'x'", levels = c("x", "b", "c", "d"))
#----------------------------------------------------------------------------
# Multiple variables
dat <- data.frame(x1.num = c(1, 2, 4, 5, 6),
x2.num = c(5, 19, 2, 6, 3),
x1.chr = c("a", "c", "f", "j", "k"),
x2.chr = c("b", "c", "a", "d", "k"),
x1.fac = factor(c("a", "b", "a", "c", "d")),
x2.fac = factor(c("b", "a", "d", "c", "e")))
# Example 4a: Recode numeric vector and attach to 'dat'
dat <- cbind(dat, rec(dat[, c("x1.num", "x2.num")], spec = "5 = 50; 19 = 190"))
# Example 4b: Alternative specification using the 'data' argument,
rec(x1.num, x2.num, data = dat, spec = "5 = 50; 19 = 190")
# Example 4c: Recode character vector and attach to 'dat'
dat <- cbind(dat, rec(dat[, c("x1.chr", "x2.chr")], spec = "'a' = 'X'"))
# Example 4d: Recode factor vector and attach to 'dat'
dat <- cbind(dat, rec(dat[, c("x1.fac", "x2.fac")], spec = "'a' = 'X'"))