exists_any | R Documentation |
Group records according to (zero or more) classifying variables. Test for
each group whether at least one (exists
) or precisely one
(exists_one
) record satisfies a condition.
exists_any(rule, by = NULL, na.rm = FALSE)
exists_one(rule, by = NULL, na.rm = FALSE)
rule |
|
by |
A bare (unquoted) variable name or a list of bare variable names, that will be used to group the data. |
na.rm |
|
A logical
vector, with the same number of entries as there
are rows in the entire data under scrutiny. If a test fails, all records in
the group are labeled with FALSE
.
Other cross-record-helpers:
contains_exactly()
,
do_by()
,
hb()
,
hierarchy()
,
is_complete()
,
is_linear_sequence()
,
is_unique()
# Test whether each household has exactly one 'head of household'
dd <- data.frame(
hhid = c(1, 1, 2, 1, 2, 2, 3 )
, person = c(1, 2, 3, 4, 5, 6, 7 )
, hhrole = c("h","h","m","m","h","m","m")
)
v <- validator(exists_one(hhrole=="h", hhid))
values(confront(dd, v))
# same, but now with missing value in the data
dd <- data.frame(
hhid = c(1, 1, 2, 1, 2, 2, 3 )
, person = c(1, 2, 3, 4, 5, 6, 7 )
, hhrole = c("h",NA,"m","m","h","m","h")
)
values(confront(dd, v))
# same, but now we ignore the missing values
v <- validator(exists_one(hhrole=="h", hhid, na.rm=TRUE))
values(confront(dd, v))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.