[R] mean for vector with NA

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Thu Aug 21 21:22:59 CEST 2008


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of 
> bad2101 at columbia.edu
> Sent: Thursday, August 21, 2008 12:15 PM
> To: r-help at r-project.org
> Subject: [R] mean for vector with NA
> 
> I am trying to find the mean for the elements in the vector
> 
> Incubation=as.POSIXlt(OnsetTime)-as.POSIXlt(MealTime)
> 
> where
> 
> OnsetTime=c(NA,"1940-04-19 00:30","1940-04-19 00:30","1940-04-19  
> 00:30",NA,"1940-04-18 22:30","1940-04-18 22:30","1940-04-19  
> 02:00","1940-04-19 01:00","1940-04-18 23:00",NA,NA,NA,"1940-04-19  
> 02:00",NA,"1940-04-19 10:30","1940-04-19 00:30","1940-04-18  
> 22:15",NA,"1940-04-18 22:00","1940-04-19 01:00","1940-04-18  
> 23:00",NA,"1940-04-18 21:45",NA,"1940-04-18 21:45","1940-04-19  
> 01:00",NA,"1940-04-18 23:00",NA,"1940-04-18 21:00","1940-04-19  
> 01:00","1940-04-19 01:00",NA,NA,"1940-04-18 21:15",NA,"1940-04-18  
> 23:30","1940-04-19 01:00","1940-04-18 21:30",NA,"1940-04-19  
> 02:30","1940-04-19 02:00","1940-04-18 21:30",NA,NA,"1940-04-19  
> 00:30","1940-04-19 01:00","1940-04-18 22:30",NA,NA,"1940-04-18  
> 15:00",NA,"1940-04-19 00:00","1940-04-18 23:00",NA,"1940-04-18  
> 22:30","1940-04-19 01:00","1940-04-19 02:30","1940-04-18  
> 23:30",NA,NA,NA,NA,"1940-04-19 01:00","1940-04-19  
> 00:30",NA,NA,NA,"1940-04-19 00:30","1940-04-19 01:00","1940-04-19  
> 00:00",NA,"1940-04-19 02:15","1940-04-18 23:00")
> MealTime=c(NA,"1940-04-18 20:00","1940-04-18 18:30","1940-04-18  
> 18:30",NA,"1940-04-18 19:30","1940-04-18 19:30","1940-04-18  
> 19:30","1940-04-18 22:00","1940-04-18 19:00",NA,NA,NA,"1940-04-18  
> 19:30",NA,NA,NA,NA,NA,NA,"1940-04-18 
> 22:00",NA,NA,NA,NA,NA,"1940-04-18  
> 22:00",NA,NA,"1940-04-18 22:00",NA,"1940-04-18 22:00","1940-04-18  
> 22:00",NA,NA,NA,NA,NA,"1940-04-18 22:00",NA,NA,NA,NA,NA,"1940-04-18  
> 22:00",NA,NA,"1940-04-18 19:00",NA,NA,NA,"1940-04-18  
> 11:00",NA,NA,NA,NA,NA,"1940-04-18 22:00","1940-04-18  
> 19:30","1940-04-18 19:30",NA,NA,NA,NA,"1940-04-18  
> 22:00",NA,"1940-04-18 19:30","1940-04-18 19:30",NA,NA,"1940-04-18  
> 19:30","1940-04-18 19:30","1940-04-18 22:00",NA,NA)
> 
> I have tried
> 
> mean(Incubation)
> 
> and
> 
> mean(as.numeric(Incubation))
> 
> but I think that, since there are so many NA values in Incubation, R  
> gives a mean value of NA.  Is there any way of either extracting all  
> numeric values (i.e. all non-NA values) from the Incubation 
> vector, or  
> finding the mean value of only the numeric values in the Incubation  
> vector?
> 
> Brian
> 

Try 

mean(Incubation, na.rm=TRUE)

Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA  98504-5204
 
 



More information about the R-help mailing list