Skip to content
Prev 300352 / 398503 Next

Script help: Determining Time Difference between two data points.

Does this do what you want?
  > firstInRun <- function(x) c(TRUE, x[-1] != x[-length(x)])
  > lastInRun <- function(x) c(x[-1] != x[-length(x)], TRUE)
  > f <- function(data, condition) {
  +     with(data, data.frame(startDate = Date[firstInRun(condition)],
  +                           endDate = Date[lastInRun(condition)]))
  + }
  > f(d, d$Score > 150)
              startDate             endDate
  1 2008-04-30 23:58:00 2008-05-01 07:35:00
  2 2008-05-01 14:20:00 2008-05-01 14:20:00
  3 2008-05-01 16:01:00 2008-05-09 16:43:00
  4 2008-05-09 18:22:00 2008-05-09 22:44:00
  5 2008-05-10 01:03:00 2008-05-12 16:01:00
  6 2008-05-12 18:09:00 2008-05-12 18:09:00
  7 2008-05-12 20:44:00 2008-05-13 09:14:00
  8 2008-05-13 12:03:00 2008-05-13 12:03:00
  > .Last.value$endDate - .Last.value$startDate
  Time differences in secs
  [1]  27420      0 693720  15720 226680      0  45000      0
  attr(,"tzone")
  [1] ""

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com