Skip to contents

check_sequential() checks whether the elements of a numeric vector follow a sequential order, starting from a specified value. The function raises an error message if the vector is not sequential. check_is_sequential() returns TRUE if the vector is sequential, and FALSE otherwise.

Usage

check_sequential(vec, start = 1, by = 1, par_name = "vec")

check_is_sequential(vec, start = start, by = by)

Arguments

vec

A numeric vector to check for sequential order.

start

The value from which the sequence should start. Defaults to 1.

by

Increament, that is, the difference between consecutive values. Use a negative value for decreasing sequence.

par_name

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

Value

  • check_sequential(): Returns the original vector entered or raises an error message if the sequence is not valid.

  • check_is_sequential(): Logical value, TRUE if the vector is sequential, and FALSE otherwise.

Examples

# Examples for check_sequential()
# -------------------------------
v <- c(1, 2, 3, 4, 5)
check_sequential(v)
#> [1] 1 2 3 4 5

v2 <- c(1, 2, 4, 5)
try(check_sequential(v2))
#> Error in check_sequential(v2) : 
#>   Expected 'vec' to be a sequential vector but got 1, 2, 4, 5

v3 <- c(10, 20, 30, 40)
check_sequential(v3, start = 10, by = 10)
#> [1] 10 20 30 40

v4 <- c(5, 4, 3, 2, 1)
check_sequential(v4, start = 5, by = -1)
#> [1] 5 4 3 2 1

v5 <- c(3, 5, 7, 9)
try(check_sequential(v4, start = 2))
#> Error in check_sequential(v4, start = 2) : 
#>   Expected 'vec' to be a sequential vector but got 5, 4, 3, 2, 1

# Examples for check_is_sequential()
# -------------------------------
check_is_sequential(v)
#> [1] FALSE
check_is_sequential(v2)
#> [1] FALSE
check_is_sequential(v3, start = 10, by = 10)
#> [1] TRUE
check_is_sequential(v4, start = 5, by = -1)
#> [1] TRUE
check_is_sequential(v5, start = 2)
#> [1] FALSE