比较后续行和查找重叠的时间间隔? [英] Comparing subsequent rows and finding overlapping time intervals?

查看:128
本文介绍了比较后续行和查找重叠的时间间隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,我们称之为Times:

I have a dataset, let's call it "Times":

> Times <- read.csv("Times.csv, header=TRUE)
> Times
Num     Start          End
1       00:09:41       00:25:025
2       00:11:21       00:41:32
3       00:34:39       00:58:01

所以这些只是几行数据,但是有近50行。

So those are just a few lines of data, but there are close to 50 rows.

我真的坚持如何找到重叠的时间间隔因此,一行的开始与下一行的结束之间的差异至少有一个值,我需要将其与所有其他行进行比较。

I'm really stuck on how to find overlapping time intervals. So that the difference between the "Start" of one row and the "End" of the next row has a value of at least one. I need it to compare each row to all other rows.

我以为它会涉及一个循环和某种条件语句,但是我在调​​试时遇到麻烦,我的输出将希望只包括那些与其他行重叠的行。

I was thinking it would involve a loop and some sort of conditional statement, but I'm having trouble debugging. My output will hopefully only include those rows that have overlap with other rows.

推荐答案

如果您将这个想法作为N x N比较,我可以想象,答案将是某种粗糙的带状矩阵(Look如果带矩阵不是你所用的术语此代码应该测试第二列高端的重叠大于第一列,即重叠:

If you were thinking of this as an N x N comparison, I would imagine that the answer would be some sort of ragged band matrix. (Look it up if band matrix is not a term you've seen before.) This code should test for overlap at the high end of the second column being greater than the first column, i.e., overlapping:

 Times <- read.table(text="
 Num     Start          End
 1       00:09:41       00:25:25
 2       00:11:21       00:41:32
 3       00:34:39       00:58:01", stringsAsFactors=FALSE, header=TRUE)
 mdat <- outer(Times$Start, Times$End, function(x,y) y > x)
 mdat[upper.tri(mdat)|col(mdat)==row(mdat)] <- NA
 mdat
#------------------
      [,1] [,2] [,3]
[1,]    NA   NA   NA
[2,]  TRUE   NA   NA
[3,] FALSE TRUE   NA

您对对角线不感兴趣,因为End始终大于开始并且测试矩阵的上三角形部分都将为TRUE。

You are not interested in the diagonal since End is always greater than Start and the upper triangular portion of the test matrix is all going to be TRUE.

这篇关于比较后续行和查找重叠的时间间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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