gotrout@gmail.com
2004-Oct-05 17:27 UTC
[R] correct my method of estimating mean of two POSIXlt data frames
Hello, I searched the archives but could not come to a solution. I have to two columns of information t_start_cdt looks 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 looks like:> 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'd like to estimate the mean of each "pair". For example, the mean of (t_start_cdt[1] and t_end_cdt[1]). The only way I could do this is: hi <- cbind(as.matrix(as.POSIXct(t_start_cdt)), as.matrix(as.POSIXct(t_end_cdt))) hi <- apply(hi, MARGIN=1, FUN=mean) class(hi) <- c("POSIXt", "POSIXct") t_mean_cdt <- as.POSIXlt(hi) rm(hi) What am I missing conceptually about POSIXlt, and what is the better method? thanks, Mike
Gabor Grothendieck
2004-Oct-05 18:57 UTC
[R] correct my method of estimating mean of two POSIXlt data frames
<gotrout <at> gmail.com> writes:
:
: Hello, I searched the archives but could not come to a solution. I
: have to two columns of information
:
: t_start_cdt looks 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 looks like:
: > 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'd like to estimate the mean of each "pair". For example, the
mean
: of (t_start_cdt[1] and t_end_cdt[1]). The only way I could do this
: is:
: hi <- cbind(as.matrix(as.POSIXct(t_start_cdt)),
: as.matrix(as.POSIXct(t_end_cdt)))
: hi <- apply(hi, MARGIN=1, FUN=mean)
: class(hi) <- c("POSIXt", "POSIXct")
: t_mean_cdt <- as.POSIXlt(hi)
: rm(hi)
:
: What am I missing conceptually about POSIXlt, and what is the better method?
: thanks,
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
POSIXct and finally to POSIXlt:
a <- unclass(as.POSIXct(a.lt))
b <- unclass(as.POSIXct(b.lt))
as.POSIXlt(structure((a+b)/2, class = c("POSIXt",
"POSIXct")))
Gabor Grothendieck
2004-Oct-05 22:17 UTC
[R] correct my method of estimating mean of two POSIXlt data frames
page took 0.31 seconds home | my page | my email
.
email
Mail Addresses Calendar Notepad ggrothendieck at myway.com sign out
<< Hide Folders Check Messages Compose Message POP Accounts | Mail
Preferences | Help
Folders
Inbox
Drafts
Sent
Trash (Empty)
Bulk Mail (Empty)
My Folders edit
R
Rcom
Rtmp
Saved
this
Tx
zoo
Spam Tools info
Spam Filter Level:
OffLowMediumMedium-highHigh
My Block List
Image Filter
Custom Filters
Now Live:
125MB Free Storage Upgrade
Send/Receive 10MB emails!
< Prev Next > Back to Inbox Print View Full Header
As AttachmentAs Inline Text Move to Folder----- Folders
------InboxDraftsSentTrashBulk Mail---- My Folders ----RRcomRtmpSavedthisTxzoo
Message is not flagged. [ Flag for Follow Up ]
If instead of a data frame of POSIXlt objects you use a matrix
of POSIXct objects then that should work. That is, replace
tmp <- data.frame(t.start.cdt, t.end.cdt)
with
tmp <- cbind(t.start.cdt+0, t.end.cdt+0)
in your code.
Date: Tue, 5 Oct 2004 14:56:41 -0700
From: <gotrout at gmail.com>
To: <r-help at stat.math.ethz.ch>
Subject: Re: [R] correct my method of estimating mean of two POSIXlt data
frames
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.