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

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Wed Aug 22 16:47:55 CEST 2018


On 22/08/2018 10:33 AM, Ivan Calandra wrote:
> Dear useRs,
> 
> I have just noticed that when input is only NA with na.rm=TRUE, mean()
> results in NaN, whereas median() and sd() produce NA. Shouldn't it all
> be the same? I think NA makes more sense than NaN in that case.

The mean can be defined as sum(x)/length(x), so if x is length 0, you 
get 0/0 which is NaN.

median(x) is documented in its help page to give NA for x of length 0.

sd(x) is documented to give an error for such x and NA for length 1, but 
it gives NA for both.

Duncan Murdoch
> 
> x <- c(NA, NA, NA) mean(x, na.rm=TRUE) [1] NaN median(x, na.rm=TRUE) [1]
> NAsd(x, na.rm=TRUE) [1] NA
> 
> Thanks for any feedback.
> 
> Best,
> Ivan
>




More information about the R-help mailing list