日期时间 - 确定多个(n)个日期时间范围是否在R中相互重叠 [英] Datetime -Determine whether multiple(n) datetime ranges overlap each other in R

查看:144
本文介绍了日期时间 - 确定多个(n)个日期时间范围是否在R中相互重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,如果多个日期时间范围相互重叠,并且如果是重叠的时间段,我有一个问题。我已经参考了以下链接确定两个日期范围重叠检测重叠期间的算法等等。

Hi friends i have a problem finding if multiple datetime ranges overlap each other and if yes the time period for which they overlap.I have refereed following links Determine Whether Two Date Ranges Overlap and Algorithm to detect overlapping periods and some more.

不知道这是否正确,我有n = 3的样本说明。

Don't know if this is right,i have sample explanation for n=3.

说我有'n'个开关sw1,sw2& sw3.State是ON / OFF状态,即1/0。

Say I have 'n' switches sw1,sw2 & sw3.State is ON/OFF state ie 1/0.

Switches,State,Intime,Outtime

sw3,1,9:00:00,10:40:00
sw2,1,9:30:00,10:15:00
sw1,1,10:00:00,11:00:00
sw2,1,10:20:00,10:30:00



我遇到这个可能性。可能会有更多的。寻找他人。常见的时间是从10:00到10:15,即15分钟,10:20到10:30,即10分钟。这些开关接通('1')的组合时间段为25分钟。

I have come across this one possibility.There might be more.Still looking for others.Here the common time period is from 10:00 to 10:15 ie 15 mins and 10:20 to 10:30 ie 10 mins.The combined time period for which these switches were ON('1') is 25 mins.

                 10:00                           11:00
              sw1 |-----------------------------------|
       9:30       10:15   10:20     10:30
     sw2 |-------------|      |-------|
 9:00                                     10:40 
sw3 |----------------------------------------| 

为n个重叠交换机推广此datetime是一项艰巨的任务。我仍然在工作,所以任何建议或者修改是受欢迎的。感谢你。

Generalizing this datetime for n overlapping switches is a difficult task.I m still working on it so any suggestions or modifications are welcomed.Thank you.

推荐答案

1)根据样本数据,我们假定数据的格式为hh:mm:00,其中hh < 24.

1) Based on the sample data we assume that the data is in the form of hh:mm:00 where hh < 24.

读入测试数据。创建两个函数,将格式为hh:mm:00的字符串转换为分钟数,并将一个将分钟数转换为c c对象的函数。为每一行数据创建一个分钟的序列,给出 Intervals 列表。将那些对应于同一个交换机的序列联合起来,给出列表 Intervals.u ,然后与该列表的组件相交,给出序列交点。在交点中计算运行 r ,以提供一组起点和终点。最后calcualte的分钟数,并转换为times类的持续时间。 (分钟数和持续时间仅取决于 r 交点,所以我们可以跳过以##结尾的行if intervals.df 不需要。)

Read in the test data. Create two functions which convert a character string of the form hh:mm:00 to number of minutes and a function which converts number of minutes to a chron "times" object. Create minute by minute sequences for each row of the data giving the Intervals list. Union those sequences which correspond to the same switch giving the list Intervals.u and then intersect the components of that list to give the sequence Intersection. Compute the runs, r, in Intersection to give a set of start and end points. Finally calcualte the number of minutes and converting that to "times" class the duration. (The number of minutes and duration only depend on r and Intersection so we could skip the lines ending in ## if intervals.df were not needed.)

# test data
Lines <- "Switches,State,Intime,Outtime
sw3,1,9:00:00,10:40:00
sw2,1,9:30:00,10:15:00
sw1,1,10:00:00,11:00:00
sw2,1,10:20:00,10:30:00"
DF <- read.csv(text = Lines, as.is = TRUE)

library(chron)

to.num <- function(x) floor(as.numeric(times(x)) * 24 * 60 + 1e-6)
to.times <- function(x) times(x / (24 * 60))

Seq <- function(r) seq(to.num(DF$Intime[r]), to.num(DF$Outtime[r]))    
Intervals <- lapply(1:nrow(DF), Seq)
Intervals.u <- lapply(split(Intervals, DF$Switches), 
     function(L) Reduce(union, L))
Intersection <- Reduce(intersect, Intervals.u)

r <- rle(c(FALSE, diff(Intersection) == 1))

i.ends <- cumsum(r$lengths)[r$values] ##
ends <- to.times(Intersection[i.ends]) ##
starts <- ends - to.times(r$lengths[r$values]) ##
intervals.df <- data.frame(start = starts, end = ends); intervals.df ##
##         start      end
##    1 10:00:00 10:15:00
##    2 10:20:00 10:30:00

mins <- length(Intersection) - sum(r$values); mins
## [1] 25
duration <- to.times(mins); duration
## [1] 00:25:00

2) / strong>关于速度的意见,我们可以使用有效编码范围的IRanges软件包,还可以轻松减少代码大小:

2) Regarding comments pertaining to speed we could, instead, use the IRanges package which encodes ranges efficiently and also reduces the code size slightly:

library(IRanges)
Intervals <- IRanges(to.num(DF$Intime), to.num(DF$Outtime))
Intersection <- Reduce(intersect, split(Intervals, DF$Switches))

intervals.df <- data.frame(start = to.times(start(Intersection)), 
                           end = to.times(end(Intersection)))
intervals.df
##      start      end
## 1 10:00:00 10:15:00
## 2 10:20:00 10:30:00

mins <- sum(width(Intersection) - 1); mins
## [1] 25
duration <- to.times(mins); duration
## [1] 00:25:00

更新一些修复和更好的变量名。进一步改进。添加(2)。

Updates Some fixes and better variable names. Further improvements. Added (2).

这篇关于日期时间 - 确定多个(n)个日期时间范围是否在R中相互重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆