coalesce: select 1st existing value of several columns

Description Usage Arguments Details Value Examples

View source: R/coalesce.R

Description

Inspired by the respectively named sql function. In a list of vectors with equal length the function for each of the observations selects the first value that is not NA, in the order provided

Usage

1

Arguments

...

vectors holding the values, sepearated by "," (commonly columns of data.frame)

Details

Data are transformed according to

b=\frac{2^M}{2^M+1}

Value

A named vector holding the supplemented values

Examples

1
2
3
4
    a1 = c(1,NA,NA,NA)
    a2 = c(2,2,NA,NA)
    a3 = c(NA,3,3,NA)
    cbind(a1,a2,a3,suppl=coalesce(a1,a2,a3))

Example output

     a1 a2 a3 suppl
[1,]  1  2 NA     1
[2,] NA  2  3     2
[3,] NA NA  3     3
[4,] NA NA NA    NA

preputils documentation built on July 1, 2020, 5:35 p.m.