chk-helper {testdat} | R Documentation |
Checks: data frame helpers
Description
These helper functions allowing easy checking using an arbitrary function
(func
) over multiple columns (vars
) of a data frame (data
), with an
optional filter (flt
).
Usage
chk_filter(data, vars, func, flt = TRUE, args = list())
chk_filter_all(data, vars, func, flt = TRUE, args = list())
chk_filter_any(data, vars, func, flt = TRUE, args = list())
Arguments
data |
A data frame to check. |
vars |
< |
func |
A function to use for checking that takes a vector as the first argument and returns a logical vector of the same length showing whether an element passed or failed. |
flt |
< |
args |
A list of additional arguments to be added to the function calls. |
Details
-
chk_filter()
appliesfunc
withargs
tovars
indata
filtered withflt
and returns a data frame containing the resulting logical vectors. -
chk_filter_all()
andchk_filter_any()
both runchk_filter()
and return a single logical vector flagging whether all or any values in each row areTRUE
(i.e. the conjunction and disjunction, respectively, of the columns in the output ofchk_filter()
).
Value
A logical vector or data frame of logical vectors flagging records
that have passed or failed the check, with NA
where records do not meet
the filter condition.
See Also
Other chk_*()
functions such as chk_values()
Examples
# Check that every 4-cylinder car has an engine displacement of < 100 cubic
# inches AND < 100 horsepower - return a data frame
chk_filter(
mtcars,
c("disp", "hp"),
chk_range,
cyl == 4,
list(min = 0, max = 100)
)
# Check that every 4-cylinder car has an engine displacement of < 100 cubic
# inches AND < 100 horsepower
chk_filter_all(
mtcars,
c("disp", "hp"),
chk_range,
cyl == 4,
list(min = 0, max = 100)
)
# Check that every 4-cylinder car has an engine displacement of < 100 cubic
# inches OR < 100 horsepower
chk_filter_any(
mtcars,
c("disp", "hp"),
chk_range,
cyl == 4,
list(min = 0, max = 100)
)
# Check that columns made up of whole numbers are binary
chk_filter_all(
mtcars,
where(~ all(. %% 1 == 0)),
chk_values,
TRUE,
list(0:1)
)