[R] correct my method of estimating mean of two POSIXlt data frames
Gabor Grothendieck
ggrothendieck at myway.com
Tue Oct 5 20:57:23 CEST 2004
<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")))
More information about the R-help
mailing list