dk_to_na: DK to NA

Description Usage Arguments Details Value Examples

View source: R/dk_to_na.R

Description

Takes a factor and converts "Don't know/Refused" responses to NA. Matches a variety of common patterns such as "Don't know", "Refused"s, "DK/Ref", etc.

Usage

1
2
3
4
5
dk_to_na(
  f,
 
    pattern = "^[Dd]on['’]t [Kk]|^[Dd][Kk]|^[Rr]efuse|^[Dd][Kk]/[Rr]ef|\\(VOL\\.\\)$"
)

Arguments

f

A factor variable.

pattern

A regular expression that matches factor levels to convert to NA.

Details

Make sure to tabulate the variable after running this function and check that levels weren't unintentionally removed.

Value

The factor variable, with DK/Ref values set to NA.

Examples

1
2
3
4
5
demonstration <- factor(c(rep("Yes", 57), rep("No", 45), rep("Don't know", 4),
                        rep("Refused", 2), rep("DK/Ref", 10), rep("Not sure (VOL.)", 5)))
tablena(demonstration)
demonstration <- dk_to_na(demonstration)
tablena(demonstration) # success!

pewresearch/pewmethods documentation built on March 27, 2020, 7:22 p.m.