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
> -----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
Thanks for all the help. I'll do some more reading. Brian
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