[R] mean for vector with NA

Roland Rau roland.rproject at gmail.com
Thu Aug 21 21:31:11 CEST 2008


Hi,

bad2101 at columbia.edu wrote:
> I have tried
> 
> mean(Incubation)
> 
> and
> 
> mean(as.numeric(Incubation))
> 
what about
mean(Incubation, na.rm=TRUE)
?

I get the following result:
 > mean(Incubation, na.rm=TRUE)
Time difference of 4.295455 hours
 >


> but I think that, since there are so many NA values in Incubation, R 
> gives a mean value of NA.

R returns (correctly) NA for such operations when NAs occur in the 
dataset. Sorry, but I don't know where to find it at the moment but 
there is standard for this.
It has nothing to do with the proportion of NAs in the dataset 
("...since there are so many NA values...")


Is there any way of either extracting all
> numeric values (i.e. all non-NA values) from the Incubation vector, 

Incubation[is.numeric(Incubation)]

but this is probably not what you want, since the NAs are also 
considered numeric:

 > is.numeric(Incubation)
[1] TRUE

or
> finding the mean value of only the numeric values in the Incubation vector?
> 
see above:
NAs are also numeric in this case
mean(Incubation[is.numeric(Incubation)])
 > mean(Incubation[is.numeric(Incubation)])
Time difference of NA hours
 >


Hope this helps,
Roland



More information about the R-help mailing list