How to implement coalesce efficiently in R

前端 未结 8 2445
深忆病人
深忆病人 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:08

    On my machine, using Reduce gets a 5x performance improvement:

    coalesce2 <- function(...) {
      Reduce(function(x, y) {
        i <- which(is.na(x))
        x[i] <- y[i]
        x},
      list(...))
    }
    
    > microbenchmark(coalesce(a,b,c),coalesce2(a,b,c))
    Unit: microseconds
                   expr    min       lq   median       uq     max neval
      coalesce(a, b, c) 97.669 100.7950 102.0120 103.0505 243.438   100
     coalesce2(a, b, c) 19.601  21.4055  22.8835  23.8315  45.419   100
    

提交回复
热议问题