recode.na: Recode values of a variable to missing values, using exact or...

View source: R/recode.R

recode.naR 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 FALSE.

regex

use regular expressions to match values that include the "*" or "|" wildcards. Defaults to TRUE.

as.numeric

coerce the recoded variable to numeric. The function recommends the option when the recode returns only numeric values. Defaults to FALSE.

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

regex

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))

questionr documentation built on Feb. 16, 2023, 10:14 p.m.