[R] correct my method of estimating mean of two POSIXlt data frames

gotrout@gmail.com gotrout at gmail.com
Tue Oct 5 23:56:41 CEST 2004


My apology for not properly quoting someone.

Let me amend my original email.  In R version 1.8.1, if I have two
objects that look like:

> t.start.cdt[1:4]
[1] "2003-07-09 11:02:25" "2003-07-09 11:10:25" "2003-07-09 11:30:25"
[4] "2003-07-09 12:00:25"
> class(t.start.cdt)
[1] "POSIXt"  "POSIXlt"

> t.end.cdt[1:4]
[1] "2003-07-09 11:02:35" "2003-07-09 11:10:35" "2003-07-09 11:30:35"
[4] "2003-07-09 12:00:35"
> class(t.end.cdt)
[1] "POSIXt"  "POSIXlt"

I could estimate the mean of "pairs" of times (such as t.start.cdt[1]
and t.end.cdt[1]) using these commands:
tmp <- data.frame(t.start.cdt, t.end.cdt)
tmp <- apply(tmp, MARGIN=1, FUN=mean)
class(tmp) <- c("POSIXt", "POSIXct")
t.mean.cdt <- as.POSIXlt(tmp)

In version 1.9.1, I get these warnings
Warning messages:
1: longer object length
        is not a multiple of shorter object length in: cl == c("Date",
"POSIXct", "POSIXlt")
2: longer object length
        is not a multiple of shorter object length in: cl == c("Date",
"POSIXct", "POSIXlt")
3: argument is not numeric or logical: returning NA in:
mean.default(newX[, i], ...)
4: argument is not numeric or logical: returning NA in:
mean.default(newX[, i], ...)

I think I wrote a rather convoluted way around this problem, which I
stated in my original email, but I don't "understand" why the method
stated here doesn't work.  Can you explain what I'm missing.

Also, please note that in my original message, I used underscore
instead of period in the variable names.

Mike
Berkeley, California, USA



On Tue, 5 Oct 2004 21:13:05 +0100 (BST), Prof Brian Ripley
<ripley at stats.ox.ac.uk> wrote:
> On Tue, 5 Oct 2004 "gotrout at gmail.com" (but with no name nor signature)
> wrote:
> 
> Quoting someone without credit (and therefore in breach of their
> copyright)
> 
> 
> 
> > > If a.lt and b.lt are the two vectors of POSIXlt dates then try
> > > converting each to POSIXct and unclassing to make each numeric.
> > > Take the mean of the two numeric vectors and convert them back to
> >
> > I see.  I'm a little confused with the use of class/unclass versus
> > as.XX.  For example, instead of using unclass, why wouldn't I use
> > as.numeric?  Could someone explain the difference?
> 
> That advice is wrong: you should not be unclassing before forming the
> mean as mean() has a method for POSIXct.
> 
> --
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
> 
>




More information about the R-help mailing list