Emmanuel Charpentier
2009-Mar-17 06:53 UTC
[R] Mean of difftime vectors : "code infelicity" or intended behaviour ?
Dear list, "+" (and "-") being defined for difftime class, I expected mean() to return something sensible. This is only half-true :> mean(c(1:5, 5:1),na.rm=TRUE)[1] 3> mean(as.difftime(c(1:5, 5:1),unit="mins"),na.rm=TRUE)Time difference of 3 mins Fine so far. However :> mean(c(1:5, NA,5:1),na.rm=TRUE)[1] 3> mean(as.difftime(c(1:5, NA,5:1),unit="mins"),na.rm=TRUE)Time difference of NA mins Ouch ! Curiously, var(), max() and min() behave as expected. What's so special with mean() ?> RSiteSearch("mean difftime")[ ... doesn't return anything relevant ] NB : this isn't done for the hell of it. I intended to replace some missing dates, with something computed from other dates and mean time intervals). Any thoughs ? Emmanuel Charpentier
Emmanuel Charpentier
2009-Mar-17 07:04 UTC
[R] Mean of difftime vectors : "code infelicity" or intended behaviour ?
Oops. Clicked "Send" too fast (don't shoot, Brian !). I forgot :> sessionInfo()R version 2.8.1 (2008-12-22) i486-pc-linux-gnu locale: LC_CTYPE=fr_FR.UTF-8;LC_NUMERIC=C;LC_TIME=fr_FR.UTF-8;LC_COLLATE=fr_FR.UTF-8; LC_MONETARY=C;LC_MESSAGES=fr_FR.UTF-8;LC_PAPER=fr_FR.UTF-8;LC_NAME=C; LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=fr_FR.UTF-8;LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices datasets utils methods base loaded via a namespace (and not attached): [1] grid_2.8.1 lattice_0.17-20 lme4_0.999375-28 Matrix_0.999375-21 [5] nlme_3.1-90>On Tue, 17 Mar 2009 06:53:20 +0000, Emmanuel Charpentier wrote?:> Dear list, > > "+" (and "-") being defined for difftime class, I expected mean() to > return something sensible. This is only half-true : > >> mean(c(1:5, 5:1),na.rm=TRUE) > [1] 3 >> mean(as.difftime(c(1:5, 5:1),unit="mins"),na.rm=TRUE) > Time difference of 3 mins > > Fine so far. However : > >> mean(c(1:5, NA,5:1),na.rm=TRUE) > [1] 3 >> mean(as.difftime(c(1:5, NA,5:1),unit="mins"),na.rm=TRUE) > Time difference of NA mins > > Ouch ! > > Curiously, var(), max() and min() behave as expected. What's so special > with mean() ? > >> RSiteSearch("mean difftime") > [ ... doesn't return anything relevant ] > > NB : this isn't done for the hell of it. I intended to replace some > missing dates, with something computed from other dates and mean time > intervals). > > Any thoughs ? > > Emmanuel Charpentier > > ______________________________________________ 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.