exclusivity-expectations {testdat} | R Documentation |
expect_exclusive
tests that vars
are exclusive - that, if any one of
vars
is set to exc_val
, no other column in vars
or var_set
is also
set to exc_val
.
expect_exclusive(vars, var_set, exc_val = 1, flt = TRUE, data = get_testdata())
vars |
< |
var_set |
< |
exc_val |
The value that flags a variable as "selected" (default: |
flt |
< |
data |
A data frame to test. The global test data is used by default. |
This expectation is designed to check exclusivity in survey multiple response sets, where one response is only valid on its own.
See the example data set below:
No record should have q10_98
, "None of the above", selected while also
having any other response selected, so we refer to this as an "exclusive"
response.
expect_exclusive()
checks whether q10_98
"None of the above" or
q10_99
"Don't know", the exclusive responses, have been selected alongside
any other q10_*
response.
The expectation fails, since the first record has both q10_1
and
q10_98
selected.
expect_*()
functions are mainly called for their side effects. The
expectation signals its result (e.g. "success", "failure"), which is logged
by the current test reporter. In a non-testing
context the expectation will raise an error with class
expectation_failure
if it fails.
Other data expectations:
conditional-expectations
,
datacomp-expectations
,
date-expectations
,
expect_depends()
,
generic-expectations
,
label-expectations
,
pattern-expectations
,
proportion-expectations
,
text-expectations
,
uniqueness-expectations
,
value-expectations
my_q_block <- data.frame(
resp_id = 1:5, # Unique to respondent
q10_1 = c(1, 1, 0, 0, 0),
q10_2 = c(0, 1, 0, 0, 0),
q10_3 = c(0, 0, 1, 0, 0),
q10_98 = c(1, 0, 0, 1, 0), # None of the above
q10_99 = c(0, 0, 0, 0, 1) # Item not answered
)
# Make sure that if "None of the above" and "Item skipped" are selected
# none of the other question options are selected:
try(
expect_exclusive(
c(q10_98, q10_99),
starts_with("q10_"),
data = my_q_block
)
)