coalesce: Replace missing values

View source: R/coalesce.R

coalesceR Documentation

Replace missing values

Description

Construct a vector with as few missing values as possible from a selected sequence of vectors.

Usage

coalesce(mat, ...)

index.coalesce(mat, ...)

Arguments

mat

a vector or matrix.

...

additional vectors or matrices, must have the same number of rows as mat. The last argument can be a constant that would substitute for all remaining missing values.

Value

For coalesce, a vector in which each element is determined by selecting the first non-missing value in the order in which they are specified in the argument list. The first step is to construct a matrix from all arguments. The output is initially set to column 1; for any missing values in the column, the data from column 2 are used and so on until all columns have been searched or all missing values replaced.

For index.coalesce, an integer vector indicating which column from mat or from the vectors or constant specified for ... produced the result in coalesce.

Note

This function is most useful for creating a column in a dataset from related columns that represent different methods. For example, a single column of alkalinity may be desired when there are multiple columns of alkalinity determined by various methods.

Examples


coalesce(c(1,NA,NA,3), c(2,2,NA,2))
# should be: [1]  1  2 NA  3
coalesce(c(1,NA,NA,3), c(2,2,NA,2), 0)
# should be: [1] 1 2 0 3

USGS-R/smwrBase documentation built on Oct. 18, 2022, 9:55 a.m.