Learn R Programming

broadcast (version 0.1.8)

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, ...)

# S3 method for default checkNA(x, op, inv = FALSE, ...)

checkNULL(x, ...)

# S3 method for default checkNULL(x, op, inv = FALSE, ...)

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.

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.

Examples

Run this code

# 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, "&")
bc.b(mx, my, "xor")
bc.b(mx, my, "nand")
bc.b(mx, my, "==")
bc.b(mx, my, "!=")
bc_ifelse(bc.b(mx, my, "|"), -1000L, x + y)


# 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, "&")
bc.b(mx, my, "xor")
bc.b(mx, my, "nand")
bc.b(mx, my, "==")
bc.b(mx, my, "!=")
bc_ifelse(bc.b(mx, my, "|"), list(~ "Nothing"), bc.list(x, y, paste0))

Run the code above in your browser using DataLab