chk-helper {testdat} | R Documentation |
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
).
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())
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. |
chk_filter()
applies func
with args
to vars
in data
filtered
with flt
and returns a data frame containing the resulting logical vectors.
chk_filter_all()
and chk_filter_any()
both run chk_filter()
and
return a single logical vector flagging whether all or any values in each
row are TRUE
(i.e. the conjunction and disjunction, respectively, of the
columns in the output of chk_filter()
).
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.
Other chk_*()
functions such as chk_values()
# 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)
)