Learn R Programming

rowr (version 1.0.1)

coalesce: A more versatile form of the T-SQL coalesce() function.

Description

Little more than a wrapper for vectorize, allows for duplication of SQL coalesce functionality, certain types of if-else statements, and apply/Reduce combinations.

Usage

coalesce(..., fun = (function(x, y) if (!is.na(x)) x else y))

Arguments

...
an arbitrary number of R objects
fun
a two argument function that returns an atomic value

Examples

Run this code
coalesce(c(NA,1,2))
coalesce(c(NA,1,2),c(3,4,NA))
df<-data.frame(a=c(NA,2,3),b=c(1,2,NA))
coalesce(df$a,df$b)
# Or even just:
coalesce(df)

Run the code above in your browser using DataLab