如何在 R 中找到不同时间段的瞬时上下文?

How do I find distinct time periods of context around instants in R?

我从一组日志时间戳开始,并希望生成一组不重叠的时间段,以便我可以在某些上下文中显示相应的日志条目。

假设我在 R 中有一个日期时间列表:

times <- c("2015-03-10 19:13:35", 
           "2015-03-10 19:13:37", 
           "2015-03-10 19:15:20", 
           "2015-03-10 19:16:40", 
           "2015-03-10 19:16:45")

我想生成一个跨越这些日期两侧 10 秒且没有重叠的时间段列表。例如:

[1] "2015-03-10 19:13:25" -- "2015-03-10 19:13:47"
[2] "2015-03-10 19:15:10" -- "2015-03-10 19:15:30"
[3] "2015-03-10 19:16:30" -- "2015-03-10 19:16:55"

我已经尝试使用 lubridate,我可以创建时间段(公认的简单部分)。如何合并重叠的时间段?

intervals <- as.interval(new_difftime(second=20), ymd_hms(times) - 10)

下面是我将如何使用基础 R

来解决这个问题
times <- as.POSIXct(times) # Convert your times to POSIXct class
Myfunc <- function(x) { 
                      temp <- range(x) ; 
                      c(min = temp[1] - 10, max = temp[2] + 10)
} # Create a range function
indx <- cumsum(c(0, diff(times)) > 10) # Create an index which separate the 
tapply(times, indx, Myfunc)  # Run the whole thing
# $`0`
#                       min                       max 
# "2015-03-10 19:13:25 IST" "2015-03-10 19:13:47 IST" 
# 
# $`1`
#                       min                       max 
# "2015-03-10 19:15:10 IST" "2015-03-10 19:15:30 IST" 
# 
# $`2`
#                       min                       max 
# "2015-03-10 19:16:30 IST" "2015-03-10 19:16:55 IST" 

或者,如果您更喜欢结构更健壮的结果,我会选择 data.table(显然 aggregate 由于某种原因不保留 POSIXct class )

df <- data.frame(times, indx)
library(data.table)
setDT(df)[, as.list(Myfunc(times)), by = indx]
#    indx                 min                 max
# 1:    0 2015-03-10 19:13:25 2015-03-10 19:13:47
# 2:    1 2015-03-10 19:15:10 2015-03-10 19:15:30
# 3:    2 2015-03-10 19:16:30 2015-03-10 19:16:55