How to implement coalesce efficiently in R

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

    Another apply method, with mapply.

    mapply(function(...) {temp <- c(...); temp[!is.na(temp)][1]}, a, b, c)
    [1]  1  2 NA  4  6
    

    This selects the first non-NA value if more than one exists. The last non-missing element could be selected using tail.

    Maybe a bit more speed could be squeezed out of this alternative using the bare bones .mapply function, which looks a little different.

    unlist(.mapply(function(...) {temp <- c(...); temp[!is.na(temp)][1]},
                   dots=list(a, b, c), MoreArgs=NULL))
    [1]  1  2 NA  4  6
    

    .mapplydiffers in important ways from its non-dotted cousin.

    • it returns a list (like Map) and so must be wrapped in some function like unlist or c to return a vector.
    • the set of arguments to be fed in parallel to the function in FUN must be given in a list to the dots argument.
    • Finally, mapply, the moreArgs argument does not have a default, so must explicitly be fed NULL.

提交回复
热议问题