是否有一种方便而优雅的现有方法来查找逻辑时间序列中包含值 True 或 1 的连续区域?我正在寻找返回表单的 ts 摘要的东西:
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
示例输入 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
这可能是二进制信号处理的常见任务,因此值得搜索。当然,它是理想化的。只是为了开始。现实会更加复杂。