From: Sebastian Luque <spluque_at_gmail.com>

Date: Wed 14 Jun 2006 - 13:16:38 EST

}, fromSeq, toSeq, from, to)

diffDims <- list(phases=paste(from, to, sep="-"),

Date: Wed 14 Jun 2006 - 13:16:38 EST

[sorry for my previous empty follow-up -- my fingers got messed up.]

Spencer Graves <spencer.graves@pdf.com> wrote:

*> Did you try the following:
**>
**>> xto-xfrom
**> Time in days:
**> [1] 1.50 1.75 2.00 2.25
*

I wanted to find the total amount of time between a series of date/time's corresponding to particular times of the day, not how much time there is between the two series. I ended my coding it as:

"subTime" <- function(from, to, t0, t1)

{

stopifnot(length(from) == length(to), length(t0) == length(t1))

startFrom <- chron(sapply(t0, "+", as.numeric(dates(from)))) endFrom <- chron(sapply(t1, "+", as.numeric(dates(from)))) startTo <- chron(sapply(t0, "+", as.numeric(dates(to))))endTo <- chron(sapply(t1, "+", as.numeric(dates(to)))) fromSeq <- mapply(seq, startFrom, startTo) toSeq <- mapply(seq, endFrom, endTo) diffs <- mapply(function(x0, x1, y0, y1) {

bad <- x1 < y0 | x0 > y1 full <- x0 >= y0 & x1 <= y1 fullDiff <- sum(x1[full] - x0[full], na.rm=TRUE) r <- x0 >= y0 & x1 > y1 & !bad x1[r] <- y1 truncR <- sum(pmax(x1[r], x0[r]) - x0[r], na.rm=TRUE) l <- x0 < y0 & x1 <= y1 & !bad x0[l] <- y0 truncL <- sum(x1[l] - pmax(x0[l], x1[l]), na.rm=TRUE) sum(fullDiff, truncR, truncL)

}, fromSeq, toSeq, from, to)

diffDims <- list(phases=paste(from, to, sep="-"),

periods=paste(t0, t1, sep="-"))matrix(diffs, ncol=length(t0), dimnames=diffDims) }

The logicals seemed to be needed to account for truncated periods. It's working as I needed it to, but it looks too convoluted so if somebody finds an easier way to do this, I'd be happy to learn about it. The function returns a matrix:

R> xfrom <- chron(seq(1.25, 11, 3.25)) R> xto <- chron(as.numeric(xfrom) + seq(1.5, 2.25, 0.25)) R> xt0 <- times(c("04:00:00", "11:00:00")) R> xt1 <- times(c("10:00:00", "16:00:00")) R> subTime(xfrom, xto, xt0, xt1) periods phases 04:00:00-10:00:00 11:00:00-16:00:00 (01/02/70 06:00:00)-(01/03/70 18:00:00) 0.2500 0.4167 (01/05/70 12:00:00)-(01/07/70 06:00:00) 0.3333 0.2083 (01/08/70 18:00:00)-(01/10/70 18:00:00) 0.5000 0.4167 (01/12/70 00:00:00)-(01/14/70 06:00:00) 0.5833 0.4167

*> Also, have you seen Gabor Grothendieck and Thomas Petzoldt. "R help
*

> desk: Date and time classes in R". R News, 4(1):29-32, June 2004.,

*> downloadable from www.r-project.org -> "Documentation: Newsletter" as
**> well as the "zoo" vignette (see
**> "http://finzi.psych.upenn.edu/R/Rhelp02a/archive/67006.html")?
*

I haven't read the "zoo" vignette yet, but the R News article is one I come back to often.

Thank you,

-- Seb ______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlReceived on Wed Jun 14 13:21:38 2006

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.1.8, at Wed 14 Jun 2006 - 14:11:26 EST.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*