【问题标题】:How do I find distinct time periods of context around instants in R?如何在 R 中的瞬间找到不同的上下文时间段?
【发布时间】:2015-04-23 16:43:55
【问题描述】:

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

假设我在 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"
  • [1] 在时间[1] 之前 10 秒开始,在时间[2] 之后 10 秒结束(因为它们彼此相距在 10 秒以内)
  • [2]times[3] 的每一边只有 10 秒,因为它与其他时期不重叠
  • [3] 在时间 [4] 之前 10 秒开始,在时间 [5] 之后 10 秒结束(它们再次接近)

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

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

【问题讨论】:

    标签: r datetime lubridate


    【解决方案1】:

    以下是我将如何使用基础 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 类)

    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
    

    【讨论】:

    • 谢谢,帮了大忙。
    猜你喜欢
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 2020-01-20
    相关资源
    最近更新 更多