[R] NA and NaN question

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Wed Jan 7 13:32:38 CET 2009


Pascal A. Niklaus wrote:
> Hi all,
>
> I ran into a problem in some of my code that could be traced back to 'mean' 
> sometimes returning NA and sometimes NaN, depending on the value of na.rm:
>
>   
>> mean(c())
>>     
> [1] NA
>
>   
>> mean(c(NA),na.rm=T)
>>     
> [1] NaN
>
> However, I don't understand the reasoning behind this and would appreciate and 
> explanation. 
>   

note the types:

typeof(c())
typeof(c(NA))
typeof(c(NA)[-na.omit(c(NA))])

now,

mean(NULL)
mean(logical(0))

mean(c())
# NA, because you take the mean of a vector of non-{numeric,logical}
type (see the warning message)
mean(c(NA), na.rm=TRUE)
# NaN, because you take the mean of a zero-length logical vector
mean(c(NA), na.rm=FALSE)
# NA, because you take the mean of a logical vector containing an NA


you can argue that ?mean underspecifies this (it doesn't say anything
about the value for a zero-length logical, numeric, or complex vector,
though you can guess it will be the value of 0/0).

vQ




More information about the R-help mailing list