Check if a vector of values is included in another vector of values
Source:R/validation-inclusion.R
isIncluded.Rd
Check if a vector of values is included in another vector of values
Usage
isIncluded(values, parentValues)
validateIsIncluded(values, parentValues, nullAllowed = FALSE)
Arguments
- values
A vector of values.
- parentValues
A vector of values where
values
are checked for inclusion.- nullAllowed
Boolean flag if
NULL
is accepted for theobject
. IfTRUE
,NULL
always returnsTRUE
, otherwiseNULL
returnsFALSE
. Default isFALSE
.
Value
isIncluded()
returnsTRUE
if the value or allvalues
(if it's a vector) are present in theparentValues
;FALSE
otherwise.validateIsIncluded()
returnsNULL
if child value is included in parent value set, otherwise error is signaled.
Examples
# check if a column is present in dataframe
A <- data.frame(
col1 = c(1, 2, 3),
col2 = c(4, 5, 6),
col3 = c(7, 8, 9)
)
isIncluded("col3", names(A)) # TRUE
#> [1] TRUE
# check if single element is present in a vector (atomic or non-atomic)
isIncluded("x", list("w", "x", 1, 2)) # TRUE
#> [1] TRUE
isIncluded("x", c("w", "a", "y")) # FALSE
#> [1] FALSE
# check if **all** values (if it's a vector) are contained in parent values
isIncluded(c("x", "y"), c("a", "y", "b", "x")) # TRUE
#> [1] TRUE
isIncluded(list("x", 1), list("a", "b", "x", 1)) # TRUE
#> [1] TRUE
isIncluded(c("x", "y"), c("a", "b", "x")) # FALSE
#> [1] FALSE
isIncluded(list("x", 1), list("a", "b", "x")) # FALSE
#> [1] FALSE
# corresponding validation
validateIsIncluded("col3", names(A)) # NULL
#> NULL
# validateIsIncluded("col6", names(A)) # error