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

Description Usage Arguments Examples

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

1
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

1
2
3
4
5
6
7
8
9
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)
# Coalesce can actually use any comparison.  For example, instead of non-NA
# values it could find the max in each row:
cbind(EuStockMarkets,Max=coalesce(EuStockMarkets,fun=function (x,y) if (x>y) x else y))


Search within the rowr package
Search all R packages, documentation and source code

Questions? Problems? Suggestions? or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.