Skip to contents

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

Usage

check_all_numeric(vmat, par_name = "vmat")

check_is_all_numeric(vmat)

Arguments

vmat

A vector or matrix to check for numeric values.

par_name

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

Value

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

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

Examples

# Examples for check_all_numeric()
# --------------------------------

check_all_numeric(c(1, 2, 3))
#> [1] 1 2 3

try(check_all_numeric(c(1, 2, "a", 3)))
#> Error in check_all_numeric(c(1, 2, "a", 3)) : 
#>   Expected all values of 'vmat' to be numeric but got 1, 2, a, 3

check_all_numeric(c(4.5, 6.7, 8.9))
#> [1] 4.5 6.7 8.9

try(check_all_numeric(c("a", "b", "c")))
#> Error in check_all_numeric(c("a", "b", "c")) : 
#>   Expected all values of 'vmat' to be numeric but got a, b, c

try(check_all_numeric(data.frame(a = 1:3, b = 4:6)))
#> Error in check_vector_or_matrix(vmat = vmat, par_name = par_name) : 
#>   Expected 'vmat' to be a vector with at least 1 element or a matrix but got data.frame

try(check_all_numeric(c(NA, "text", 4)))
#> Error in check_all_numeric(c(NA, "text", 4)) : 
#>   Expected all values of 'vmat' to be numeric but got NA, text, 4

check_all_numeric(c(1L, 2L, 3L))
#> [1] 1 2 3

# Examples for check_is_all_numeric()
# -----------------------------------

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

check_is_all_numeric(c(1, 2, "a", 3))
#> [1] FALSE

check_is_all_numeric(c(4.5, 6.7, 8.9))
#> [1] TRUE

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

try(check_is_all_numeric(data.frame(a = 1:3, b = 4:6)))
#> [1] FALSE

check_is_all_numeric(c(NA, "text", 4))
#> [1] FALSE

check_is_all_numeric(c(1L, 2L, 3L))
#> [1] TRUE