Learn R Programming

assertive (version 0.3-0)

assert_are_set_equal: Set comparisons

Description

Checks on the contents of two vectors (ignoring the order of the elements).

Usage

assert_are_set_equal(x, y)

assert_is_set_equal(x, y)

assert_is_subset(x, y)

assert_is_superset(x, y)

is_set_equal(x, y, .xname = get_name_in_parent(x),
  .yname = get_name_in_parent(y))

is_subset(x, y, .xname = get_name_in_parent(x),
  .yname = get_name_in_parent(y))

is_superset(x, y, .xname = get_name_in_parent(x),
  .yname = get_name_in_parent(y))

Arguments

x
A vector.
y
Another vector.
.xname
Not intended to be used directly.
.yname
Not intended to be used directly.

Value

  • The is_* functions return TRUE or FALSE. The assert_* functions throw an error in the event of failure.

See Also

is_subset, sets, set_is_equal

Examples

Run this code
# Same contents, different order, returns TRUE
is_set_equal(1:5, 5:1)
# Different lengths
is_set_equal(1:5, 1:6)
# First vector contains values not in second vector
is_set_equal(1:5, c(1:4, 4))
# Second vector contains values not in first vector
is_set_equal(c(1:4, 4), 1:5)

# Is x a subset of y?
is_subset(1:4, 1:5)
is_subset(1:5, 1:4)

# Is x a superset of y?
is_superset(1:5, 1:4)
is_superset(1:4, 1:5)

# Types are coerced to be the same, as per base::setdiff
is_set_equal(1:4, c("4", "3", "2", "1"))

# Errors are thrown in the event of failure
assert_are_set_equal(1:5, 5:1)
dont_stop(assert_are_set_equal(1:5, 1:6))

assert_is_subset(1:4, 1:5)
dont_stop(assert_is_subset(1:5, 1:4))

assert_is_superset(1:5, 1:4)
dont_stop(assert_is_superset(1:4, 1:5))

# A common use case: checking that data contains required columns
required_cols <- c("Time", "weight", "Diet")
assert_is_superset(colnames(ChickWeight), required_cols)

Run the code above in your browser using DataLab