Neelay Trivedi Neelay Trivedi - 11 months ago 55
R Question

What purpose does na.rm serve inside of mean() in R?

I am trying to understand why na.rm would be used inside of the mean() function in R. Specifically, I am trying to figure out the difference between na.rm =TRUE and na.rm=FALSE.


na.rm = TRUE takes out any NA values that could potentially be in the vector you are taking the mean of. If there is even 1 NA value in your vector and na.rm = FALSE the result will be NA. Here's a basic example:

a <- c(1,2,3,4,NA,6)

> mean(a, na.rm = FALSE)
[1] NA

If you choose na.rm = TRUE it will take the mean of all the values in the vector that aren't NA:

> mean(a, na.rm = TRUE)
[1] 3.2

Note that if you don't include the na.rm argument in your function the default is na.rm = FALSE.

> mean(a)
[1] NA