[R] NA and NaN question

Pascal A. Niklaus pniklaus at ethz.ch
Wed Jan 7 13:45:56 CET 2009


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:
> 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.
> >
> > I understand that the mean of an empty vector is not definied,
>
> Not so, it is well-defined as 0/0 = NaN.
>
> > but I don't
> > understand why it matters whether the vector was empty from the beginning
>
> 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 Niklaus
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html and provide commented,
> > minimal, self-contained, reproducible code.




More information about the R-help mailing list