exists_any: Test for (unique) existence

View source: R/syntax.R

exists_anyR Documentation

Test for (unique) existence

Description

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.

Usage

exists_any(rule, by = NULL, na.rm = FALSE)

exists_one(rule, by = NULL, na.rm = FALSE)

Arguments

rule

[expression] A validation rule

by

A bare (unquoted) variable name or a list of bare variable names, that will be used to group the data.

na.rm

[logical] Toggle to ignore results that yield NA.

Value

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.

See Also

Other cross-record-helpers: contains_exactly(), do_by(), hb(), hierarchy(), is_complete(), is_linear_sequence(), is_unique()

Examples

# 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))


validate documentation built on March 24, 2022, 5:07 p.m.