Skip to contents

check_constant_vector() and check_is_constant_vector() functions check if a given vector has constant values within a specified tolerance. check_constant_vector() raises an error if the vector's values are not constant within the tolerance, while check_is_constant_vector() returns a logical value indicating if the vector is constant.

Usage

check_constant_vector(vec, tol = 1e-16, par_name = "vec")

check_is_constant_vector(vec, tol = 1e-16, par_name = "vec")

Arguments

vec

A vector to check if it is constant.

tol

A tolerance value to determine if the vector is constant.

par_name

An optional name of the parameter to display in error messages.

Value

  • check_constant_vector(): Returns original value entered or raises an error message if an error occurs or any of the conditions is not met.

  • check_is_constant_vector(): Logical value, TRUE if the vector is constant within the tolerance, and FALSE otherwise.

Examples

# Examples for check_constant_vector()
# ------------------------------------

check_constant_vector(c(3, 3, 3))
#> [1] 3 3 3

try(check_constant_vector(c(1, 2, 3)))
#> Error in check_constant_vector(c(1, 2, 3)) : 
#>   Expected 'vec' to be a constant vector but got a vector with different values.

try(check_constant_vector(c(5.0001, 5.0002, 5.0001)))
#> Error in check_constant_vector(c(5.0001, 5.0002, 5.0001)) : 
#>   Expected 'vec' to be a constant vector but got a vector with different values.

check_constant_vector(c(5.0001, 5.0002, 5.0001), tol = 0.001)
#> [1] 5.0001 5.0002 5.0001

try(check_constant_vector(c(NA, NA, NA)))
#> Error in check_vector(vec = vec, n = NULL, inequality = ">=", par_name = par_name) : 
#>   Expected 'vec' to be a numeric vector but got logical

try(check_constant_vector(c("a", "b", "c")))
#> Error in check_vector(vec = vec, n = NULL, inequality = ">=", par_name = par_name) : 
#>   Expected 'vec' to be a numeric vector but got character

# Examples for check_is_constant_vector()
# ---------------------------------------

check_is_constant_vector(c(3, 3, 3))
#> [1] TRUE

check_is_constant_vector(c(1, 2, 3))
#> [1] FALSE

check_is_constant_vector(check_constant_vector(c(5.0001, 5.0002, 5.0001)))
#> [1] FALSE

check_is_constant_vector(c(5.0001, 5.0002, 5.0001), tol = 0.001)
#> [1] TRUE

check_is_constant_vector(c(NA, NA, NA))
#> [1] FALSE

check_is_constant_vector(c("a", "b", "c"))
#> [1] FALSE