recodes
recodes the values of one or more variables in
a data frame
recodes(data, vars, from, to)
a data frame.
character vector of variable names.
a vector of values or conditions (see Details).
a vector of replacement values.
a data frame
For each variable in the vars
parameter, values
are checked against the list of values in the from
vector.
If a value matches, it is replaced with the corresponding
entry in the to
vector.
Once a given observation's value matches a from
value, it is
recoded. That particular observation will not be recoded again by
that recodes()
statement (i.e., no chaining).
One or more values in the from
vector can be an expression,
using the dollar sign ($) to represent the variable being recoded.
If the expression
evaluates to TRUE
, the corresponding to
value is
returned.
If the number of values in the to
vector is less than
the from
vector, the values are recycled. This lets you
convert several values to a single outcome value (e.g., NA
).
If the to
values are numeric, the resulting recoded variable
will be numeric. If the variable being recoded is a factor and the
to
values are character values, the resulting variable will
remain a factor. If the variable being recoded is a character variable
and the to
values are character values, the resulting
variable will remain a character variable.
See the vignette for detailed examples.