
This function recodes a numeric vector, character vector, or factor according to recode specifications.
rec(x, spec, as.factor = FALSE, levels = NULL, as.na = NULL, table = FALSE,
check = TRUE)
a numeric vector, character vector or factor.
a character string of recode specifications (see 'Details').
logical: if TRUE
, character vector will be coerced to a factor.
a character vector for specifying the levels in the returned factor.
a numeric vector indicating user-defined missing values,
i.e. these values are converted to NA
before conducting the analysis.
logical: if TRUE
, a cross table variable x recoded variable is printed
on the console.
logical: if TRUE
, argument specification is checked.
Returns a numeric vector with the same length as x
containing the recoded variable.
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, 0 = NA
- vector of values For example, c(7, 8, 9) = 'high'
- range of values
For example, 7:9 = 'C'. The special values lo (lowest value) and hi (highest value) may appear in a range.
For example, lo:10 = 1
. Note that :
is not the R sequence operator. In addition you may not
use :
with the collect operator, e.g., c(1, 3, 5:7)
will cause an error.
- else
For example, else = NA
. Everything that does not fit a previous specification. Note that
else
matches all otherwise unspecified values on input, including NA
.
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/
# NOT RUN {
#--------------------------------------
# Numeric vector
x.num <- c(1, 2, 4, 5, 6, 8, 12, 15, 19, 20)
# Recode 5 = 50 and 19 = 190
rec(x.num, "5 = 50; 19 = 190")
# Recode 1, 2, and 5 = 100 and 4, 6, and 7 = 200 and else = 300
rec(x.num, "c(1, 2, 5) = 100; c(4, 6, 7) = 200; else = 300")
# Recode lowest value to 10 = 100 and 11 to highest value = 200
rec(x.num, "lo:10 = 100; 11:hi = 200")
# Recode 5 = 50 and 19 = 190 and check recoding
rec(x.num, "5 = 50; 19 = 190", table = TRUE)
#--------------------------------------
# Character vector
x.chr <- c("a", "c", "f", "j", "k")
# Recode a to x
rec(x.chr, "'a' = 'X'")
# Recode a and f to x, c and j to y, and else to z
rec(x.chr, "c('a', 'f') = 'x'; c('c', 'j') = 'y'; else = 'z'")
# Recode a to x and coerce to a factor
rec(x.chr, "'a' = 'X'", as.factor = TRUE)
#--------------------------------------
# Factor
x.factor <- factor(c("a", "b", "a", "c", "d", "d", "b", "b", "a"))
# Recode a to x, factor levels ordered alphabetically
rec(x.factor, "'a' = 'x'")
# Recode a to x, user-defined factor levels
rec(x.factor, "'a' = 'x'", levels = c("x", "b", "c", "d"))
# }
Run the code above in your browser using DataLab