Skip to contents

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

Usage

check_all_missing(vmat, par_name = "vmat")

check_is_all_missing(vmat)

Arguments

vmat

A vector or matrix to check for missing values.

par_name

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

Value

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

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

Examples

# Examples for check_all_missing()
# --------------------------------

check_all_missing(c(NA, NA, NA))
#> [1] NA NA NA

try(check_all_missing(c(1, NA, 3, NA)))
#> Error in check_all_missing(c(1, NA, 3, NA)) : 
#>   Expected all values of 'vmat' to be missing numeric but got 2 non-missing values

A1 <- matrix(NA, nrow = 3, ncol = 3)
print(A1)
#>      [,1] [,2] [,3]
#> [1,]   NA   NA   NA
#> [2,]   NA   NA   NA
#> [3,]   NA   NA   NA
check_all_missing(A1)
#>      [,1] [,2] [,3]
#> [1,]   NA   NA   NA
#> [2,]   NA   NA   NA
#> [3,]   NA   NA   NA

A2 <- matrix(c(1, NA, 3, NA, 5, 6), nrow = 2)
print(A2)
#>      [,1] [,2] [,3]
#> [1,]    1    3    5
#> [2,]   NA   NA    6
try(check_all_missing(A2))
#> Error in check_all_missing(A2) : 
#>   Expected all values of 'vmat' to be missing numeric but got 4 non-missing values

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

df <- data.frame(a = 1:3, b = c(NA, 2, 3))
print(df)
#>   a  b
#> 1 1 NA
#> 2 2  2
#> 3 3  3
try(check_all_missing(df))
#> Error in check_all_missing(df) : 
#>   Expected 'vmat' to be a numeric vector or matrix but got data.frame

# Examples for check_is_all_missing()
# -----------------------------------

check_is_all_missing(c(NA, NA, NA))
#> [1] TRUE

check_is_all_missing(c(1, NA, 3, NA))
#> [1] FALSE

print(A1)
#>      [,1] [,2] [,3]
#> [1,]   NA   NA   NA
#> [2,]   NA   NA   NA
#> [3,]   NA   NA   NA
check_is_all_missing(A1)
#> [1] TRUE

print(A2)
#>      [,1] [,2] [,3]
#> [1,]    1    3    5
#> [2,]   NA   NA    6
check_is_all_missing(A2)
#> [1] FALSE

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

print(df)
#>   a  b
#> 1 1 NA
#> 2 2  2
#> 3 3  3
check_is_all_missing(df)
#> [1] FALSE