Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R组ID';时间间隔重叠的时间间隔_R - Fatal编程技术网

R组ID';时间间隔重叠的时间间隔

R组ID';时间间隔重叠的时间间隔,r,R,我有一个包含多个组的大型数据集,在具有开始和停止日期时间的IDs数据集中。我要做的是在每个组中确定子组发生的位置。当两个ID与其开始和结束日期时间列重叠时,一个组中的子组将被删除。下面是在R中为一个组创建示例数据集的脚本。我要做的是在每个组中创建一个名为“Grp”的列,该列将那些具有重叠开始和结束日期时间的子组分组 我所拥有的 structure(list(ID = c(1,2,3,4), START = structure(c(1490904000, 1490918400, 15083631

我有一个包含多个组的大型数据集,在具有开始和停止日期时间的IDs数据集中。我要做的是在每个组中确定子组发生的位置。当两个ID与其开始和结束日期时间列重叠时,一个组中的子组将被删除。下面是在R中为一个组创建示例数据集的脚本。我要做的是在每个组中创建一个名为“Grp”的列,该列将那些具有重叠开始和结束日期时间的子组分组

我所拥有的

structure(list(ID = c(1,2,3,4), START = structure(c(1490904000, 1490918400, 
1508363100, 1508379300), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), END = structure(c(1492050600, 1492247700, 
1509062400, 1509031800), tzone = "UTC", class = c("POSIXct", 
"POSIXt"))), class = "data.frame", row.names = c(NA, -4L), .Names = c("ID","START", 
"END")) 
我想要的是

structure(list(ID = c(1,2,3,4), START = structure(c(1490904000, 1508379300, 
1508363100, 1490918400), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), END = structure(c(1492050600, 1509031800, 
1509062400, 1492247700), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), Grp = c(1,2,2,1)), class = "data.frame", row.names = c(NA, -4L), .Names = c("ID","START", 
"END","Grp"))

我尝试过使用lubridate的间隔,并通过这种方式找到重叠,但没有成功。如果有任何帮助,我们将不胜感激。

在按开始排序后,新组的条件是上一行的结尾小于下一组的开头:

head(df1$END, -1) < tail(df1$START,-1)

df1 <- structure(list(ID = c(1,2,3,4), START = structure(c(1490904000, 1490918400, 
1508363100, 1508379300), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), END = structure(c(1492050600, 1492247700, 
1509062400, 1509031800), tzone = "UTC", class = c("POSIXct", 
"POSIXt"))), class = "data.frame", row.names = c(NA, -4L), .Names = c("ID","START", 
"END"))

df1
  ID               START                 END
1  1 2017-03-30 20:00:00 2017-04-13 02:30:00
2  2 2017-03-31 00:00:00 2017-04-15 09:15:00
3  3 2017-10-18 21:45:00 2017-10-27 00:00:00
4  4 2017-10-19 02:15:00 2017-10-26 15:30:00


df1a <- df1[ order(df1$START), ]
df1a$grp <- cumsum( c( 1, head(df1$END, -1) < tail(df1$START,-1) ))
 df1a
#---------------
  ID               START                 END grp
1  1 2017-03-30 20:00:00 2017-04-13 02:30:00   1
2  2 2017-03-31 00:00:00 2017-04-15 09:15:00   1
3  3 2017-10-18 21:45:00 2017-10-27 00:00:00   2
4  4 2017-10-19 02:15:00 2017-10-26 15:30:00   2
head(df1$END,-1)df1这确实回答了我的问题,谢谢,但是您知道如何修改它,使其在一个组中使用3个ID,在另一个组中使用2个ID?创建一个接受两个日期时间向量并返回grp向量的函数一点也不困难。然后使用by()函数或lappy(split(…)函数在因子分组中运行该函数。在BaseR数据管理中,将结果分离的数据帧绑定在一起的通常方法是对结果运行do.call(rbind,…)。当然也有一些dplyr策略会做同样的操作,但我是一个“传统主义者”。搜索“[r]在组内应用函数”。非常感谢!我感谢所有的帮助!
grp_overlaps <- function(endings, beginnings){ 
                   cumsum(c( 1, head(endings, -1) < tail(beginnings, -1) )) }