recode.na {questionr} | R Documentation |
Recode values of a variable to missing values, using exact or regular expression matching.
Description
This function recodes selected values of a quantitative or qualitative variable by matching its levels to exact or regular expression matches.
Usage
recode.na(x, ..., verbose = FALSE, regex = TRUE, as.numeric = FALSE)
Arguments
x |
variable to recode. The variable is coerced to a factor if necessary. |
... |
levels to recode as missing in the variable. The values are coerced to character strings, meaning that you can pass numeric values to the function. |
verbose |
print a table of missing levels before recoding them as missing. Defaults to |
regex |
use regular expressions to match values that include the "*" or "|" wildcards. Defaults to |
as.numeric |
coerce the recoded variable to |
Value
The result is a factor with properly encoded missing values. If the recoded variable contains only numeric values, it is converted to an object of class numeric
.
Author(s)
François Briatte <f.briatte@gmail.com>
See Also
Examples
data(hdv2003)
## With exact string matches.
hdv2003$nivetud = recode.na(hdv2003$nivetud, "Inconnu")
## With regular expressions.
hdv2003$relig = recode.na(hdv2003$relig, "[A|a]ppartenance", "Rejet|NSP")
## Showing missing values.
hdv2003$clso = recode.na(hdv2003$clso, "Ne sait pas", verbose = TRUE)
## Test results with freq.
freq(recode.na(hdv2003$trav.satisf, "Equilibre"))
## Truncate a count variable (recommends numeric conversion).
freq(recode.na(hdv2003$freres.soeurs, 5:22))