coalesce

How to implement coalesce efficiently in R

拜拜、爱过 提交于 2019-11-25 21:59:45
问题 Background Several SQL languages (I mostly use postgreSQL) have a function called coalesce which returns the first non null column element for each row. This can be very efficient to use when tables have a lot of NULL elements in them. I encounter this in a lot of scenarios in R as well when dealing with not so structured data which has a lot of NA\'s in them. I have made a naive implementation myself but it is ridiculously slow. coalesce <- function(...) { apply(cbind(...), 1, function(x) {