| datacomp-expectations {testdat} | R Documentation |
Expectations: comparisons
Description
These functions allow for comparison between two data frames.
Usage
expect_valmatch(
data2,
vars,
by,
not = FALSE,
flt = TRUE,
data = get_testdata()
)
expect_subset(data2, by = NULL, not = FALSE, flt = TRUE, data = get_testdata())
Arguments
data2 |
The data frame to compare against. |
vars |
< |
by |
A character vector of columns to join by. See |
not |
Reverse the results of the check? |
flt |
< |
data |
A data frame to test. The global test data is used by default. |
Details
-
expect_valmatch()compares the observations appearing in one data frame (data) to the same observations, as picked out by a key (by), in another data frame (data2). It fails if the selected columns (vars) aren't the same for those observations in both data frames. -
expect_subset()compares one data frame (data) to another (data2) and fails if all of the observations in the first, as picked out by a key (by), do not appear in the second.
Value
expect_*() functions are mainly called for their side effects. The
expectation signals its result (e.g. "success", "failure"), which is logged
by the current test reporter. In a non-testing
context the expectation will raise an error with class
expectation_failure if it fails.
See Also
Other data expectations:
conditional-expectations,
date-expectations,
exclusivity-expectations,
expect_depends(),
generic-expectations,
label-expectations,
pattern-expectations,
proportion-expectations,
text-expectations,
uniqueness-expectations,
value-expectations
Examples
df1 <- data.frame(
id = 0:99,
binomial = sample(0:1, 100, TRUE),
even = abs(0:99%%2 - 1) * 0:99
)
df2 <- data.frame(
id = 0:99,
binomial = sample(0:1, 100, TRUE),
odd = 0:99%%2 *0:99
)
# Check that same records 'succeeded' across data frames
try(expect_valmatch(df2, binomial, by = "id", data = df1))
# Check that all records in `df1`, as picked out by `id`, exist in `df2`
expect_subset(df2, by = "id", data = df1)