Skip to content

NA and NaN question

5 messages · Brian Ripley, Wacek Kusnierczyk, Pascal A. Niklaus

#
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:
[1] NA
[1] NaN

However, I don't understand the reasoning behind this and would appreciate and 
explanation. 

I understand that the mean of an empty vector is not definied, but I don't 
understand why it matters whether the vector was empty from the beginning or 
only after removing the NAs.

Pascal Niklaus
#
Pascal A. Niklaus wrote:
Not so, it is well-defined as 0/0 = NaN.
You didn't try that case:  mean(numeric(0)) is also NaN.  The issue is that

 > typeof(c())
[1] "NULL"

is not numeric (not evan a vector), and so mean() of it is undefined.

 > or only after removing the NAs.

Speculation (and wrong).

  
    
#
Pascal A. Niklaus wrote:
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
#
Thanks, now I understand what's happening. Maybe a line explaining this could 
be added to the help text for mean?

Pascal Niklaus
On Wed 07-Jan-2009 13:29:25 Prof Brian Ripley wrote:
#
Wacek Kusnierczyk wrote:
wrong, an artifact of some experimenting; should have been:

typeof(na.omit(c(NA)))

vQ