Skip to contents

check_all_positive() and check_is_all_positive() functions check whether all elements in a given vector or matrix are positive. check_all_positive() raises an error message if vmat contains any non-positive value(s), while check_is_all_positive() returns TRUE if all values are positive, and FALSE otherwise.

Usage

check_all_positive(vmat, is_strictly_positive = TRUE, par_name = "vmat")

check_is_all_positive(vmat, is_strictly_positive = TRUE)

Arguments

vmat

A vector or matrix to check for positive values.

is_strictly_positive

A logical value. If TRUE, checks for strictly positive values. Defaults to TRUE.

par_name

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

Value

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

  • check_is_all_positive(): Logical value, TRUE if all values are positive, and FALSE otherwise.

Examples

# Examples for check_all_positive()
# ---------------------------------

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

try(check_all_positive(c(-1, 2, 3, -4, 5)))
#> Error in check_all_positive(c(-1, 2, 3, -4, 5)) : 
#>   Expected all values of 'argument' to be strictly positive but got 2 negative / zero values

check_all_positive(c(0.1, 2.5, 3.7, 4.9, 5.0))
#> [1] 0.1 2.5 3.7 4.9 5.0

try(check_all_positive(c(0, 2, 3, 4, 5), is_strictly_positive = TRUE))
#> Error in check_all_positive(c(0, 2, 3, 4, 5), is_strictly_positive = TRUE) : 
#>   Expected all values of 'argument' to be strictly positive but got 1 negative / zero value

try(check_all_positive(c(-1, -2, -3, -4, -5)))
#> Error in check_all_positive(c(-1, -2, -3, -4, -5)) : 
#>   Expected all values of 'argument' to be strictly positive but got 5 negative / zero values

try(check_all_positive(c(1L, 2L, 0L, 4L, 5L)))
#> Error in check_all_positive(c(1L, 2L, 0L, 4L, 5L)) : 
#>   Expected all values of 'argument' to be strictly positive but got 1 negative / zero value

# Examples for check_is_all_positive()
# ------------------------------------

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

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

check_is_all_positive(c(0.1, 2.5, 3.7, 4.9, 5.0))
#> [1] TRUE

check_is_all_positive(c(0, 2, 3, 4, 5), is_strictly_positive = TRUE)
#> [1] FALSE

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

check_is_all_positive(c(1L, 2L, 0L, 4L, 5L))
#> [1] FALSE