DescTools (version 0.99.14)

Coalesce: Return the First Element Not Being NA

Description

Return the first element of a vector, not being NA.

Usage

Coalesce(..., method = c("is.na", "is.finite"))

Arguments

...
the elements to be evaluated. This can either be a single vector, several vectors of same length, a matrix, a data.frame or a list of vectors (of same length). See examples.
method
one out of "is.na" (default) or "is.finite". The "is.na" option allows Inf values to be in the result, the second one eliminates them.

Value

  • return a single vector of the first non NA element(s) of the given data structure.

Details

The evaluation will be rowwise. The first element of the result is the first non NA element of the first elements of all the arguments, the second element of the result is the one of the second elements of all the arguments and so on. Shorter inputs (of non-zero length) are NOT recycled. The idea is borrowed from SQL. Might sometimes be useful when preparing data in R instead of in SQL.

See Also

is.na, is.finite

Examples

Run this code
Coalesce(c(NA, NA, NA, 5, 3))
Coalesce(c(NA, NULL, "a"))
Coalesce(NULL, 5, 3)

d.frm <- data.frame(matrix(c(
  1,2,NA,4,
  NA,NA,3,1,
  NaN,2,3,1,
  NA,Inf,1,1), nrow=4, byrow=TRUE) 
)

Coalesce(d.frm)
Coalesce(as.matrix(d.frm))
Coalesce(d.frm[,2], d.frm[,3:4])
Coalesce(list(d.frm[,1], d.frm[,2]))

# returns the first finite element
Coalesce(d.frm, method="is.finite")

Run the code above in your browser using DataCamp Workspace