coalesce: Find first non-missing element

View source: R/coalesce.R

coalesceR Documentation

Find first non-missing element

Description

Lightweight version of dplyr::coalesce, with all the vices and virtues that come from such an approach. Very similar logic (and timings to dplyr::coalesce), though no ability to use quosures etc. One exception is that if x does not contain any missing values, it is returned immediately, and ignores .... For example, dplyr::coalesce(1:2, 1:3) is an error, but hutils::coalesce(1:2, 1:3) is not.

Usage

coalesce(x, ...)

Arguments

x

A vector

...

Successive vectors whose values will replace the corresponding values in x if the value is (still) missing.

Value

x with missing values replaced by the first non-missing corresponding elements in .... That is, if ... = A, B, C and x[i] is missing, then x[i] is replaced by A[i]. If x[i] is still missing (i.e. A[i] was itself NA), then it is replaced by B[i], C[i] until it is no longer missing or the list has been exhausted.

Source

Original source code but obviously inspired by dplyr::coalesce.

Examples

coalesce(c(1, NA, NA, 4), c(1, 2, NA, NA), c(3, 4, 5, NA))

hutils documentation built on April 13, 2022, 5:23 p.m.