【问题标题】:How to compare the two time intervals in R? [duplicate]如何比较R中的两个时间间隔? [复制]
【发布时间】:2015-12-05 02:16:12
【问题描述】:

我有这样一个时间间隔不均匀的数据。

x=data.frame(date=rep('2014-07-24',5),from=c("14:12","14:12","14:30","14:24","14:32"),to=c("15:25","15:40","15:35","15:50","15:55"),Load=c(2,2,1,1,1))

“from”和“to”列表示该时间间隔内相应负载波动的开始和结束时间。 我想将此数据转换为相应日期的 15 分钟间隔(96 个块)。因此,如果 14:15-14:30 的时间间隔存在于该时间间隔(从 - 到),它将被分配该负载值。如果它也存在于另一个区间,则该区间的负载值将进一步增加。

R中是否有任何方法可以比较这个区间00:00-00:15(和其他)是否存在于12:40-13:45这样的不均匀区间中,以便我可以相应地安排数据上面这样。

y=data.frame(date=rep('2014-07-24'),block=c("14:15-14:30","14:30-14:45","14:45-15:00","15:00-15:15","15:15-15:30"),load=c(4,7,7,7,7))

请帮忙。 非常感谢

【问题讨论】:

  • 查看foverlaps / findInterval / IRange
  • 感谢 foverlaps 以某种方式工作。
  • 请为未来遇到相同问题的用户添加您的解决方案。

标签: r time timestamp


【解决方案1】:

我能够解决我发布的问题。感谢@zx8754 foverlaps 的建议

x=data.table(date=rep('2014-07-24',5),from=c("14:12","14:12","14:30","14:24","14:32"),
         to=c("15:25","15:40","15:35","15:50","15:55"),Load=c(2,2,1,1,1))

 library(chron)

 x$from=times(paste0(as.character(x$from),":00"))
 x$to=times(paste0(as.character(x$to),":00"))

 min=15
 interval=min/(60*24)
 a=seq(from=times('14:15:00'),to=times('15:15:00'),by=interval)
 b=seq(from=times('14:30:00'),to=times('15:30:00'),by=interval)

 x2=data.table(from=a,to=b)

 setkey(x2,from,to)

 f=foverlaps(x,x2,type='any',which=TRUE)

 #### following loop is to obtain the load

 x2$load=0

 for (i in unique(f$yid)){

    xid=f$xid[f$yid==i]

        for (j in xid) {
          x2$load[i]=x2$load[i]+x$Load[j]
    }

  }

输出如下:

    y=data.frame(date=rep('2014-07-24'),block=c("14:15-14:30","14:30-
    14:45","14:45-15:00","15:00-15:15","15:15-15:30"),load=c(7,7,7,7,7))

我不明白为什么 foverlaps 将 14:15-14:30 计入 14:32-15:55。这就是为什么第一行的负载是7。

欢迎提出改进此解决方案的建议。

【讨论】:

  • 发布了一个不需要 for 循环的答案。 HTH。如果您还有其他问题,请告诉我。
【解决方案2】:

使用 data.table 中的foverlaps 我会按如下方式处理它:

1) 为两个数据表获取正确的日期时间列:

x[, `:=` (from = as.POSIXct(paste(date,from)), to = as.POSIXct(paste(date,to)), date = NULL)]
y[, c("start","end") := tstrsplit(block, "-", fixed=TRUE)
  ][, `:=` (start = as.POSIXct(paste(date,start)), 
            end = as.POSIXct(paste(date,end)), 
            block = NULL, date = NULL)]

2)设置按键:

setkey(x, from, to)
setkey(y, start, end)

3) 寻找xy 之间的重叠并获取最大值:

x.new <- foverlaps(y, x, type = "within")[, .(load.new = max(pmax(Load,load))),
                                          by = .(from, to)]

这些步骤导致:

> x.new
                  from                  to load.new
1: 2014-07-24 14:12:00 2014-07-24 15:25:00        7
2: 2014-07-24 14:12:00 2014-07-24 15:40:00        7
3: 2014-07-24 14:24:00 2014-07-24 15:50:00        7
4: 2014-07-24 14:30:00 2014-07-24 15:35:00        7
5: 2014-07-24 14:32:00 2014-07-24 15:55:00        7

使用过的数据:

x <- data.table(date=rep('2014-07-24',5),
                from=c("14:12","14:12","14:30","14:24","14:32"),
                to=c("15:25","15:40","15:35","15:50","15:55"),
                Load=c(2,2,1,1,1))
y <- data.table(date=rep('2014-07-24'),
                block=c("14:15-14:30","14:30-14:45","14:45-15:00","15:00-15:15","15:15-15:30"),
                load=c(4,7,7,7,7))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-09
    • 2018-01-10
    • 1970-01-01
    • 2013-04-22
    • 2014-01-03
    • 2020-06-18
    • 2020-07-16
    • 1970-01-01
    相关资源
    最近更新 更多