[R] To many NA's from mean(..., na.rm=T) when a column is all NA's

Peter Dalgaard p.dalgaard at biostat.ku.dk
Mon Jun 13 19:19:54 CEST 2005


Jim Robison-Cox <jimrc at math.montana.edu> writes:

>  Summary:
>   If I have a data frame or a matrix where one entire column is NA's,
> mean(x, na.rm=T) works on that column, returning NaN, but fails using
> apply, in that apply returns NA for ALL columns.
>   lapply works fine on the data frame.
> 
>   If you wonder why I'm building data frames with columns that could be
> all missing -- they arise as output of a simulation.  The fact that the
> entire column is missing is informative in itself.
> 
> 
>   I do wonder if this is a bug.

It isn't...

Cutting a long story short:

> testcase <- data.frame( x = 1:3, y = rep(NA,3))
> as.matrix(testcase)
  x   y
1 "1" NA
2 "2" NA
3 "3" NA
> testcase <- data.frame( x = 1:3, y = as.numeric(rep(NA,3)))
> as.matrix(testcase)
  x  y
1 1 NA
2 2 NA
3 3 NA
> apply(testcase,2,mean,na.rm=T)
  x   y
  2 NaN


-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list