[R] differing behavior of mean(), median() and sd() with na.rm

Rolf Turner r@turner @end|ng |rom @uck|@nd@@c@nz
Thu Aug 23 10:31:52 CEST 2018


On 08/23/2018 06:15 PM, Ivan Calandra wrote:

> Thanks all for the enlightenment.
> 
> So, it does make sense that mean() produces NaN and median()/sd() NA, 
> from a calculation point of view at least.
> But I still think it also makes sense that the mean of NA is NA as well, 
> be it only for consistency with other functions. That's just my opinion 
> of course. I can still convert NaN to NA at the end if I need to.

But the mean of NA *is* NA!

> x <- NA
> mean(x)
> [1] NA

This is *not* the same scenario as having nothing left after *removing* 
all NAs:

> x <- rep(NA,3)
> mean(x,na.rm=TRUE > [1] NaN

Seems quite consistent/coherent to me.

cheers,

Rolf Turner

-- 
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276




More information about the R-help mailing list