assert_values: Assert that a data.frame's columns are non-NA/infinite, or...

Description Usage Arguments Value Examples

View source: R/assert_values.R

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

1
2
3
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

  • not_na: All values must not be Na

  • not_nan: All values must not be NaN

  • not_inf: All values must not be infinite

  • lt: All values must be less than test_val

  • lte: All values must be less than or equal to test_val

  • gt: All values must be greater than test_val

  • gte: All values must be greater than or equal to test_val

  • equal: All values must be equal to test_val

  • not_equal: All values must not equal test_val

  • in: All values must be one of the values in test_val

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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)

assertable documentation built on Jan. 27, 2021, 9:07 a.m.