checkmissing

Efficiently Check for (Non-)Missing Values

Description

The checkNULL() function efficiently checks for the presence (or absence) of NULL in every element of a list.
The checkNA() function efficiently checks for the presence (or absence) of NA/NaN in every element of an atomic vector.

Usage

checkNA(x, ...)

## Default S3 method:
checkNA(x, op, inv = FALSE, ...)

checkNULL(x, ...)

## Default S3 method:
checkNULL(x, op, inv = FALSE, ...)

Arguments

x an object.
For checkNULL(): a recursive vector or array (i.e. type of list).
For checkNA(): an atomic vector or array (raw type not supported).
… further arguments passed to or from methods.
op

a single string, giving the operation to use.
The following operations are supported:

  • "logical": checks for every element of x the presence/absence of missing values; returns a logical vector/array.

  • "raw": same as operation "logical", except the result will be a raw vector/array (01 = TRUE, 00 = FALSE), which requires less memory.

  • "any": checks if any element is (not) missing.

  • "all": checks if all elements are (not) missing.

  • "count" or "sum": counts the number of elements that are (not) missing.

  • "which": gives the element indices that are (not) missing.

  • "first": gives the element index of the first (non-) missing element.

  • "last": gives the element index of the last (non-) missing element.

inv Boolean, indicating if the check should be inverted.
If inv = FALSE (default), the operations check for missing elements.
If inv = TRUE, the operations check for NOT missing elements.

Value

Output depends on the specification of argument op:

  • "logical": logical vector with the same length, names, and dimensions as x.

  • "raw": raw vector with the same length, names, and dimensions as x.

  • "any": TRUE or FALSE.

  • "all": TRUE or FALSE.

  • "count" or "sum": 53 bit integer scalar.

  • "which": vector of indices.

  • "first": the first index found, or 0 otherwise.

  • "last": the last index found, or 0 otherwise.

Examples

library("broadcast")


# checkNA ====
x <- array(
  sample(c(-10:10, NA, NaN)), dim = 4:2
)
y <- array(
  sample(c(-10:10, NA, NaN)), dim = c(4,1,1)
)
broadcaster(x) <- broadcaster(y) <- TRUE

mx <- checkNA(x, "raw")
my <- checkNA(y, "raw")
bc.b(mx, my, "&")
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]   00   00   00
#> [2,]   00   00   00
#> [3,]   00   00   00
#> [4,]   00   00   00
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]   00   00   00
#> [2,]   00   00   00
#> [3,]   00   00   00
#> [4,]   00   00   00
#> 
#> broadcaster
bc.b(mx, my, "xor")
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]   00   01   00
#> [2,]   00   00   00
#> [3,]   00   00   00
#> [4,]   00   00   00
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]   00   00   00
#> [2,]   00   00   00
#> [3,]   01   00   00
#> [4,]   00   00   00
#> 
#> broadcaster
bc.b(mx, my, "nand")
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]   01   00   01
#> [2,]   01   01   01
#> [3,]   01   01   01
#> [4,]   01   01   01
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]   01   01   01
#> [2,]   01   01   01
#> [3,]   00   01   01
#> [4,]   01   01   01
#> 
#> broadcaster
bc.b(mx, my, "==")
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]   01   00   01
#> [2,]   01   01   01
#> [3,]   01   01   01
#> [4,]   01   01   01
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]   01   01   01
#> [2,]   01   01   01
#> [3,]   00   01   01
#> [4,]   01   01   01
#> 
#> broadcaster
bc.b(mx, my, "!=")
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]   00   01   00
#> [2,]   00   00   00
#> [3,]   00   00   00
#> [4,]   00   00   00
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]   00   00   00
#> [2,]   00   00   00
#> [3,]   01   00   00
#> [4,]   00   00   00
#> 
#> broadcaster
bc_ifelse(bc.b(mx, my, "|"), -1000L, x + y)
#> , , 1
#> 
#>      [,1]  [,2] [,3]
#> [1,]    6 -1000    9
#> [2,]   -1    -5   -8
#> [3,]   18    -1    6
#> [4,]  -17    -4   -9
#> 
#> , , 2
#> 
#>       [,1] [,2] [,3]
#> [1,]    10    4    7
#> [2,]     9   -4   -6
#> [3,] -1000    5   -2
#> [4,]   -10   -7   -6
#> 
#> broadcaster


# checkNULL ====
x <- array(
  sample(list(letters, LETTERS, month.abb, month.name, NULL)), dim = 4:2
)
y <- array(
  sample(list(letters, LETTERS, month.abb, month.name, NULL)), dim = c(4,1,1)
)
broadcaster(x) <- broadcaster(y) <- TRUE

mx <- checkNULL(x, "raw")
my <- checkNULL(y, "raw")
bc.b(mx, my, "&")
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]   00   00   00
#> [2,]   00   00   00
#> [3,]   00   00   00
#> [4,]   00   00   00
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]   00   00   00
#> [2,]   00   00   00
#> [3,]   00   00   00
#> [4,]   01   00   00
#> 
#> broadcaster
bc.b(mx, my, "xor")
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]   01   00   00
#> [2,]   00   01   00
#> [3,]   00   00   01
#> [4,]   01   01   01
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]   00   00   01
#> [2,]   00   00   00
#> [3,]   00   00   00
#> [4,]   00   01   01
#> 
#> broadcaster
bc.b(mx, my, "nand")
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]   00   01   01
#> [2,]   01   00   01
#> [3,]   01   01   00
#> [4,]   00   00   00
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]   01   01   00
#> [2,]   01   01   01
#> [3,]   01   01   01
#> [4,]   00   00   00
#> 
#> broadcaster
bc.b(mx, my, "==")
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]   00   01   01
#> [2,]   01   00   01
#> [3,]   01   01   00
#> [4,]   00   00   00
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]   01   01   00
#> [2,]   01   01   01
#> [3,]   01   01   01
#> [4,]   01   00   00
#> 
#> broadcaster
bc.b(mx, my, "!=")
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]   01   00   00
#> [2,]   00   01   00
#> [3,]   00   00   01
#> [4,]   01   01   01
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]   00   00   01
#> [2,]   00   00   00
#> [3,]   00   00   00
#> [4,]   00   01   01
#> 
#> broadcaster
bc_ifelse(bc.b(mx, my, "|"), list(~ "Nothing"), bc.list(x, y, paste0))
#> , , 1
#> 
#>      [,1]         [,2]         [,3]        
#> [1,] ~"Nothing"   character,26 character,26
#> [2,] character,26 ~"Nothing"   character,26
#> [3,] character,12 character,12 ~"Nothing"  
#> [4,] ~"Nothing"   ~"Nothing"   ~"Nothing"  
#> 
#> , , 2
#> 
#>      [,1]         [,2]         [,3]        
#> [1,] character,26 character,26 ~"Nothing"  
#> [2,] character,26 character,26 character,26
#> [3,] character,26 character,26 character,12
#> [4,] ~"Nothing"   ~"Nothing"   ~"Nothing"  
#> 
#> broadcaster