assert_values {assertable} | R Documentation |
Assert that a data.frame's columns are non-NA/infinite, or are greater, less than, equal/not-equal, or contain specified values.
Description
Given a data.frame or data.table object, make assertions about values of the columns within the object. Assert that a column contains no missing/infinite values, or that it is greater/less than, equal to, or contains either a single value, vector with nrow(data) values, or a vector of any length(for in option).
Usage
assert_values(data, colnames, test = "not_na", test_val = NA,
display_rows = TRUE, na.rm = FALSE, warn_only = FALSE,
quiet = FALSE)
Arguments
data |
A data.frame or data.table |
colnames |
Character vector with column names corresponding to columns in data |
test |
The type of evaluation you want to assert in your data
|
test_val |
A single value, a vector with length = nrow(data), or a vector of any length (if using the in option for test. Must match the character type of colnames. |
display_rows |
Do you want to show the actual rows that violate the assertion? Default=T |
na.rm |
Do you want to remove NA and NaN values from assertions? Default=F |
warn_only |
Do you want to warn, rather than error? Will return all offending rows from the first violation of the assertion Default=F |
quiet |
Do you want to suppress the printed messages when a test is passed? Default = F. |
Value
Throws error if test is violated. If warn_only=T, will return all offending rows from the first violation of the assertion.
Examples
assert_values(CO2, colnames="uptake", test="gt", 0) # Are all values greater than 0?
assert_values(CO2, colnames="conc", test="lte", 1000) # Are all values less than/equal to 1000?
## Not run:
assert_values(CO2, colnames="uptake", test="lt", 40) # Are all values less than 40?
# Fails: not all values < 40.
## End(Not run)
assert_values(CO2, colnames="Treatment", test="in", test_val = c("nonchilled","chilled"))
CO2_mult <- CO2
CO2_mult$new_uptake <- CO2_mult$uptake * 2
assert_values(CO2, colnames="uptake", test="equal", CO2_mult$new_uptake/2)
## Not run:
assert_values(CO2, colnames="uptake", test="gt", CO2_mult$new_uptake/2, display_rows=F)
# Fails: uptake !> new_uptake/2
## End(Not run)