Check if the differences between elements of a vector are constant
Source:R/check_diff_constant.R
check-diff-constant.Rd
check_diff_constant()
and check_is_diff_constant()
verify if
the differences between elements of a numeric vector are constant.
check_diff_constant()
raises an error if the differences are not
constant, while check_is_diff_constant()
returns TRUE
if the
differences are constant and FALSE
otherwise.
Usage
check_diff_constant(vec, n = 2, tol = 1e-16, par_name = "vec")
check_is_diff_constant(vec, n = 2, tol = 1e-16)
Value
check_diff_constant()
: Returns the original vector if the differences are constant or raises an error if they are not.check_is_diff_constant()
: Logical value,TRUE
if the differences are constant, andFALSE
otherwise.
Examples
# Examples for check_diff_constant()
# ----------------------------------
vec1 <- c(1, 3, 5, 7)
print(vec1)
#> [1] 1 3 5 7
check_diff_constant(vec1)
#> [1] 1 3 5 7
vec2 <- c(2, 4, 8, 16)
print(vec2)
#> [1] 2 4 8 16
try(check_diff_constant(vec2))
#> Error in check_diff_constant(vec2) :
#> Difference between elements of 'vec' must be constant, but got 2, 4, 8, 16
vec3 <- seq(10, 20, by = 2)
print(vec3)
#> [1] 10 12 14 16 18 20
check_diff_constant(vec3)
#> [1] 10 12 14 16 18 20
vec4 <- c(1, 2, 3, 4, 7)
print(vec4)
#> [1] 1 2 3 4 7
try(check_diff_constant(vec4))
#> Error in check_diff_constant(vec4) :
#> Difference between elements of 'vec' must be constant, but got 1, 2, 3, 4, 7
vec5 <- c(0.1, 0.2, 0.3, 0.4)
print(vec5)
#> [1] 0.1 0.2 0.3 0.4
check_diff_constant(vec5, tol = 1e-15)
#> [1] 0.1 0.2 0.3 0.4
# Examples for check_is_diff_constant()
# -------------------------------------
check_is_diff_constant(vec1)
#> [1] TRUE
check_is_diff_constant(vec2)
#> [1] FALSE
check_is_diff_constant(vec3)
#> [1] TRUE
check_is_diff_constant(vec4)
#> [1] FALSE
check_is_diff_constant(vec5, tol = 1e-15)
#> [1] TRUE