How to implement coalesce efficiently in R

前端 未结 8 2467
深忆病人
深忆病人 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-21 23:56

    From data.table >= 1.12.3 you can use fcoalesce.

    library(data.table)
    fcoalesce(a, b, c)
    # [1]  1  2 NA  4  6
    

    For more info, including a benchmark, see NEWS item #18 for development version 1.12.3.

提交回复
热议问题