validate (version 0.9.3)

exists_any: 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, ..., na.rm = FALSE)

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

Arguments

rule

[expression] A validation rule

...

A comma-separated list of variables 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: is_complete(), is_unique()

Examples

Run this code
# NOT RUN {
# 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))

# }

Run the code above in your browser using DataCamp Workspace