Check that a vector contains only certain values.
chk_equals(x, val)
chk_values(x, ..., miss = getOption("testdat.miss"))
chk_range(x, min, max, ...)
chk_blank(x)
A vector to check.
A scalar value for the equality check.
Vectors of valid values.
A vector of values to be treated as missing. The testdat.miss or testdat.miss_text option is used by default.
Minimum value for range check.
Maximum value for range check.
A logical vector flagging records that have passed or failed the check.
Other vector checks:
chk-dates
,
chk-dummy
,
chk-labels
,
chk-patterns
,
chk-text
,
chk-uniqueness
x <- c(NA, 0, 1, 0.5, 0, NA, 99)
chk_blank(x) # Blank
#> [1] TRUE FALSE FALSE FALSE FALSE TRUE FALSE
chk_equals(x, 0) # Either blank or 0
#> [1] TRUE TRUE FALSE FALSE TRUE TRUE FALSE
chk_values(x, 0, 1) # Either blank, 0, 1, or 99
#> [1] TRUE TRUE TRUE FALSE TRUE TRUE FALSE
chk_range(x, 0, 1) # Either blank or in [0,1]
#> [1] TRUE TRUE TRUE TRUE TRUE TRUE FALSE
chk_range(x, 0, 1, 99) # Either blank, in [0,1], or equal to 99
#> [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE