Is it possible to set na.rm to TRUE globally?

前端 未结 4 1254
情歌与酒
情歌与酒 2020-12-05 13:56

For commands like max the option na.rm is set by default to FALSE. I understand why this is a good idea in general, but I\'d like to t

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 14:54

    For my R package, I overwrote the existing functions mean and sum. Thanks to the great Ben (comments below), I altered my functions to this:

    mean <- function(x, ..., na.rm = TRUE) {
      base::mean(x, ..., na.rm = na.rm)
    }
    

    After this, mean(c(2, NA, 3)) = 2.5 instead of NA.

    And for sum:

    sum <- function(x, ..., na.rm = TRUE) {
      base::sum(x, ..., na.rm = na.rm)
    }
    

    This will yield sum(c(2, NA, 3)) = 5 instead of NA.

    sum(c(2, NA, 3, NaN)) also works.

提交回复
热议问题