some_of {checkthat} | R Documentation |
Check if logical conditions are met some of the time in a logical vector
Description
Designed as a helper function for check_that()
, this function
allows you to check that a certain percentage or count of TRUE values are
observed in a logical vector. It is therefore a more flexible version of
all()
or any()
.
Usage
some_of(logical_vec, ...)
Arguments
logical_vec |
A logical vector to be checked. |
... |
A set of one or more frequency specifiers (e.g.,
|
Details
This function is designed as a helper function for check_that()
. It
allows you to validate that a certain percentage or count of TRUE values are
observed in a logical vector. It is therefore a more flexible version of
all()
or any()
.
The named arguments in ...
should correspond to quantifiers (e.g.,
at_least
, at_most
) followed by a numeric value representing
the criteria for that quantifier (either an integer count or proportion
between zero and one). For example, at_least = 2
checks if at least 2
TRUE values are present in logical_vec
.
Note, specifying exactly 1 in an argument is ambiguous (e.g.,
at_least = 1
). Because it could represent a count (n = 1) or a
proportion (100%), this value is not allowed in some_of()
and will
throw an error. If you need to specify exactly 1 (either as a count or a
proportion), please use a more specific quantifier function, such as
at_least(logical_vec, p = 1)
or at_least(logical_vec, n = 1)
.
Value
A logical value indicating all conditions specified in ...
resolve to TRUE in the given logical_vec
.
See Also
Other special quantifiers:
for_case()
,
whenever()
Examples
logical_vec <- c(TRUE, FALSE, TRUE, FALSE, TRUE)
# Check if at least 2 TRUE values are present
some_of(logical_vec, at_least = 2) # TRUE
# Check if at most 2 TRUE values are present
some_of(logical_vec, at_most = 2) # FALSE
# Check if exactly 3 TRUE values are present
some_of(logical_vec, exactly_equal = 3) # TRUE
# Check if exactly 4 TRUE values are present
some_of(logical_vec, exactly_equal = 3) # FALSE
# Invalid usage: No specific quantifiers provided (error will be thrown)
try(some_of(logical_vec)) # Error