Recodes a numeric vector, character vector, or factor
according to simple recode specifications.
is an alias for
recode that avoids name clashes
with packages, such as Hmisc, that have a
1 2 3
numeric vector, character vector, or factor.
character string of recode specifications: see below.
return a factor; default is
an optional argument specifying the order of the levels in the returned factor; the default is to use the sort order of the level names.
arguments to be passed to
Recode specifications appear in a character string, separated by
semicolons (see the examples below), of the form
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 on input and output.
Several recode specifications are supported:
7:9='C'. The special values
hi may appear in a range. For example,
not the R sequence operator. In addition, you may not use
: with the
c function within a recode specification, so for example
c(1, 3, 5:7) will cause an error.
everything that does not fit a previous specification.
else=NA. Note that
else matches all otherwise
unspecified values on input, including
Character data and factor levels on the left-hand side of a recode specification must be quoted. Thus,
c(a, b, c) = 'low' is not allowed, and should be
c('a', 'b', 'c') = 'low'.
Similarly, the colon is reserved for numeric data, and, e.g.,
c('a':'c') = 'low' is not allowed.
var argument is a character variable with (some) values that are composed of numerals, or a factor
with (some) levels that are numerals (e.g.,
'-2'), then these too must be quoted
and cannot be used with colons (e.g.,
'15':'19' = '15 to 19' is not allowed, and could be
c('15', '16', '17', '18', '19') = '15 to 19', assuming that all values are
the character representation of whole numbers).
If all of the output values are numeric, and if
FALSE, then a numeric result is returned; if
var is a factor,
then by default so is the result.
a recoded vector of the same length as
John Fox email@example.com
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
1 2 3 4 5 6 7 8
 1 2 3 1 2 3 1 2 3  "A" "A" "B" "A" "A" "B" "A" "A" "B"  "A" "A" "B" "A" "A" "B" "A" "A" "B"
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.