Is there a difference between na.rm = FALSE and na.ram = na.rm?

Viewed 1640

in sum() and mean(), na.rm = TRUE has the function ignore any NA values.

if na.rm = FALSE, the function includes the NA values in the calculation.

    sum_to_one <- function(x, na.rm = FALSE) {
    x / sum(x, na.rm = na.rm)
    }

This comes from R4DS, learning about functions and setting na.rm to true or false in the input. THe above code is supposed to be the same as

     x / sum(x, na.rm = TRUE)

But the na.rm is TRUE in the original expression, but in the function it is set to na.rm = FALSE in the function input and then na.rm = na.rm in the sum() expression.

I'm seeing that it is better practice to do function(x, na.rm = FALSE) {} in general to allow the user to change it and to be consistent with default settings for sum and mean. Is this correct?

1 Answers

Many base functions (base as in base R or base to any particular package) accept the argument na.rm=, where the default is often FALSE. (Some functions use useNA= or na.action, depending on different actions, but we'll ignore those.)

Higher-level functions (user-defined and/or other packages) might also define this argument and then pass it on to the other functions. For example:

parent_func <- function(x, ..., na.rm = FALSE) {
  # something important
  mu <- mean(x, na.rm = na.rm)
  sigma <- sd(x, na.rm = na.rm)
  (mu - x) / sigma
}

One premise being that if you intend to remove/ignore NA values for one portion of the function, you might use it in other places (or all).

In this case, in the call to mean(x, na.rm = na.rm), the left na.rm is referring to the argument named na.rm in the definition of mean. The right na.rm is referring to the same-named argument of parent_func.

An alternative way to define this parent function (for the sake of differentiating variables) could be:

parent_func <- function(x, ..., NARM = FALSE) {
  # something important
  mu <- mean(x, na.rm = NARM)
  sigma <- sd(x, na.rm = NARM)
  (mu - x) / sigma
}

The advantage of using na.rm= instead of this NARM= is likely consistency (though that is not always one of R's strengths across all functions). Many users are likely more intuitively familiar with the na.rm= argument name, purpose, and effect than something else.

Edit:

I'm seeing that it is better practice to do function(x, na.rm = FALSE) {} in general to allow the user to change it and to be consistent with default settings for sum and mean. Is this correct?

I believe so. In general I find that removal of missing data should be an explicit act by the user, not a default by the function. That is, if having missing data indicates a larger problem, then defaulting to na.rm=FALSE will quickly indicate to the user that something is wrong; na.rm=TRUE will mask this problem and suggest valid results when perhaps there should be no NAs at all. This holds true for the "smaller" functions (e.g., mean, sum) and so its logic should be carried outwards to the encapsulating functions.

Related