Skip to contents

check_length_equal() and check_is_length_equal() functions check whether two given vectors have the same length. check_length_equal() raises an error message if the vectors x and y do not have the same length, while check_is_length_equal() returns TRUE if they have equal lengths and FALSE otherwise.

Usage

check_length_equal(
  x,
  y,
  allow_scalar = TRUE,
  is_numeric = NULL,
  xpar_name = "x",
  ypar_name = "y"
)

check_is_length_equal(x, y, allow_scalar = FALSE, is_numeric = NULL)

Arguments

x

A vector to compare the length.

y

A vector to compare the length.

allow_scalar

Logical, whether scalar values are allowed.

is_numeric

Optional, whether to check that the vectors are numeric.

xpar_name

A name for the parameter x to display in error messages.

ypar_name

A name for the parameter y to display in error messages.

Value

  • check_length_equal(): Returns a list containing x and y if they have the same length, or raises an error message if their lengths differ.

  • check_is_length_equal(): Logical value, TRUE if x and y have equal lengths, FALSE otherwise.

Examples

# Examples for check_length_equal()
# ---------------------------------

check_length_equal(c(1, 2, 3), c(4, 5, 6))
#> $x
#> [1] 1 2 3
#> 
#> $y
#> [1] 4 5 6
#> 

try(check_length_equal(c(1, 2, 3), c(4, 5)))
#> Error in check_length_equal(c(1, 2, 3), c(4, 5)) : 
#>   Expected 'x' and 'y' to have the same number of elements but got 3 and 2 elements respectively

try(check_length_equal(c(1, 2, 3), c("a", "b", "c")))
#> $x
#> [1] 1 2 3
#> 
#> $y
#> [1] "a" "b" "c"
#> 

check_length_equal(c(1, 2), c(3, 4))
#> $x
#> [1] 1 2
#> 
#> $y
#> [1] 3 4
#> 

try(check_length_equal(c(1), c(1, 2, 3)))
#> Error in check_length_equal(c(1), c(1, 2, 3)) : 
#>   Expected 'x' and 'y' to have the same number of elements but got 1 and 3 elements respectively

# Examples for check_is_length_equal()
# ------------------------------------

check_is_length_equal(c(1, 2, 3), c(4, 5, 6))
#> [1] TRUE

check_is_length_equal(c(1, 2, 3), c(4, 5))
#> [1] FALSE

check_is_length_equal(c(1, 2), c(3, 4))
#> [1] TRUE

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

check_is_length_equal(c(1), c(1))
#> [1] FALSE