How to implement coalesce efficiently in R

前端 未结 8 2453
深忆病人
深忆病人 2020-11-21 23:20

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 b

8条回答
  •  孤独总比滥情好
    2020-11-22 00:00

    A very simple solution is to use the ifelse function from the base package:

    coalesce3 <- function(x, y) {
    
        ifelse(is.na(x), y, x)
    }
    

    Although it appears to be slower than coalesce2 above:

    test <- function(a, b, func) {
    
        for (i in 1:10000) {
    
            func(a, b)
        }
    }
    
    system.time(test(a, b, coalesce2))
    user  system elapsed 
    0.11    0.00    0.10 
    
    system.time(test(a, b, coalesce3))
    user  system elapsed 
    0.16    0.00    0.15 
    

    You can use Reduce to make it work for an arbitrary number of vectors:

    coalesce4 <- function(...) {
    
        Reduce(coalesce3, list(...))
    }
    

提交回复
热议问题