Utility function that recodes variables based on user recode specifications.
Handles both numeric or factor variables.
recode(var, recodes, afr, anr = TRUE, levels)
Arguments
| var |
Variable to recode |
| recodes |
Character string of recode specifications:
Recode specifications in a character string separated by
semicolons of the form input=output as in:
"1=1;2=1;3:6=2;else=NA"
If an input value satisfies more than one specification, then the
first (reading from left to right) is applied
If no specification is satisfied, then the input value is carried
over to the result unchanged
NA is allowed on both input and output
The following recode specifications are supported:
| Specification | Example | Notes |
| Single values | 9=NA | |
| Set of values | c(1,2,5)=1 | The left-hand-side is any R function call that returns a vector |
| seq(1,9,2)='odd' | |
| 1:10=1 | |
| Range of values | 7:9=3 | Special values lo and hi may be used |
| lo:115=1 | |
| Other values | else=NA | |
Character values are quoted as in :
recodes = "c(1,2,5)='sanitary' else='unsanitary'"
The output may be the (scalar) result of a function call as in:
recodes = "999=median(var, na.rm = TRUE)"
Users are advised to carefully check the results of recode() calls
with any outputs that are the results of a function call.
The output may be the (scalar) value of a variable as in:
recodes = "999=scalarVariable"
If all of the output values are numeric, and if 'afr' is FALSE,
then a numeric result is returned; if var is a factor then
(by default) so is the result.
|
| afr |
Return a factor. Default is TRUE if var is a factor and is
FALSE otherwise |
| anr |
Coerce result to numeric (default is TRUE) |
| levels |
Order of the levels in the returned factor; the default is to use
the sort order of the level names. |
Value
Recoded variable