This function recodes selected values of a quantitative or qualitative variable by matching its levels to exact or regular expression matches.

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.

See also

Author

François Briatte <f.briatte@gmail.com>

Examples

data(hdv2003)
## With exact string matches.
hdv2003$nivetud = recode.na(hdv2003$nivetud, "Inconnu")
#> Recoded 0 values to NA.
## With regular expressions.
hdv2003$relig = recode.na(hdv2003$relig, "[A|a]ppartenance", "Rejet|NSP")
#> Recoded 1292 values to NA.
## Showing missing values. 
hdv2003$clso = recode.na(hdv2003$clso, "Ne sait pas", verbose = TRUE)
#> Recoded 27 values to NA.
#>              n
#> Ne sait pas 27
## Test results with freq.
freq(recode.na(hdv2003$trav.satisf, "Equilibre"))
#> Recoded 451 values to NA.
#>                   n    % val%
#> Satisfaction    480 24.0 80.4
#> Insatisfaction  117  5.9 19.6
#> NA             1403 70.2   NA
## Truncate a count variable (recommends numeric conversion).
freq(recode.na(hdv2003$freres.soeurs, 5:22))
#> Recoded 505 values to NA.
#> Recoded variable contains only numeric characters. Consider using as.numeric = TRUE.
#>      n    % val%
#> 0  167  8.3 11.2
#> 1  407 20.3 27.2
#> 2  427 21.3 28.6
#> 3  284 14.2 19.0
#> 4  210 10.5 14.0
#> NA 505 25.2   NA