Is there a convenient and elegant existing approach to find contiguous regions in logical time series containing values True or 1? I am looking for something returning ts summary of the form:
Region_id Start Stop
1 YYYY-MM-DD HH:MM:SS YYYY-MM-DD HH:MM:SS
2 YYYY-MM-DD HH:MM:SS YYYY-MM-DD HH:MM:SS
... etc
Example input ts:
mins <- function (N, from = as.character(Sys.time()), cols = 1, by = 1)
{
deltas <- seq(from = 0, by = 60 * by, length.out = N)
nacol <- matrix(data = NA, ncol = cols, nrow = N)
xts(x = nacol, order.by = strptime(from, format = "%Y-%m-%d %H:%M") +
deltas)
}
d <- mins(N=20,cols=1)
d[,1] <- F; d[5:12,1] <- T; d[14:20,1] <- T
d
[,1]
2012-12-18 20:48:00 FALSE
2012-12-18 20:49:00 FALSE
2012-12-18 20:50:00 FALSE
2012-12-18 20:51:00 FALSE
2012-12-18 20:52:00 TRUE
2012-12-18 20:53:00 TRUE
2012-12-18 20:54:00 TRUE
2012-12-18 20:55:00 TRUE
2012-12-18 20:56:00 TRUE
2012-12-18 20:57:00 TRUE
2012-12-18 20:58:00 TRUE
2012-12-18 20:59:00 TRUE
2012-12-18 21:00:00 FALSE
2012-12-18 21:01:00 TRUE
2012-12-18 21:02:00 TRUE
2012-12-18 21:03:00 TRUE
2012-12-18 21:04:00 TRUE
2012-12-18 21:05:00 TRUE
2012-12-18 21:06:00 TRUE
2012-12-18 21:07:00 TRUE
# so far for the _idealized_ input, now the function I am looking for to return data.frame
# like this for the d object as above:
Region_id Start Stop
1 2012-12-18 20:52:00 2012-12-18 20:59:00
2 2012-12-18 21:01:00 2012-12-18 21:07:00
That is probably common task for binary signal processing so it is worth of searching. Of course, it is idealized. Just for start. The reality will be more complex.