我有一个数据集,它是一整年的事件集合(每天多个事件)。变量包括日期,开始时间和结束时间(以及其他内容)。我试图找出每天有多少事件重叠。为了使我的循环更简单,我首先按日期排序我的excel文件,然后按开始时间排序。我对循环有一般的想法,但无法弄清楚如何使它与R兼容。
我的想法是初始化一个空数组来计算时间重叠, 循环每个日期, 循环遍历每个事件的开始时间, 检查下一个事件是否重叠, 将1添加到我的时间重叠计数器数组。 通过这种方式,我可以看到每天有多少事件与另一个事件重叠。
非常感谢任何帮助。
overlaps = NULL
for (n in date) {
for (i in start.time) {
if (start.time[i] < start.time[i+1]
|| end.time[i] < start.time[i+1]) {
overlaps = overlaps + 1
}
}
}
答案 0 :(得分:0)
以下是我的数据集的一部分:
dates <- as.POSIXct(c("2015-01-02", "2015-01-02", "2015-01-02", "2015-01-05", "2015-01-05", "2015-01-05", "2015-01-05", "2015-01-05", "2015-01-05", "2015-01-05", "2015-01-05", "2015-01-05", "2015-01-05", "2015-01-05", "2015-01-05", "2015-01-05"), format="%Y-%m-%d")
start.time <- c("8:04", "12:31", "22:33", "7:55", "9:56", "12:35", "12:36", "14:00", "14:05", "14:10", "16:53", "20:47", "21:54", "22:00", "22:09", "23:23")
start.time <- as.POSIXct(paste(dates, start.time, sep = " "))
end.time <- c("9:12", "13:16", "23:08", "8:35", "10:44", "13:30", "13:14", "14:45", "14:43", "14:42", "17:23", "21:25", "22:32", "22:45", "22:31", "23:57")
end.time <- as.POSIXct(paste(dates, end.time, sep = " "))
events <- data.frame(dates, start.time, end.time)
##output##
dates start.time end.time
1 2015-01-02 2015-01-02 08:04:00 2015-01-02 09:12:00
2 2015-01-02 2015-01-02 12:31:00 2015-01-02 13:16:00
3 2015-01-02 2015-01-02 22:33:00 2015-01-02 23:08:00
4 2015-01-05 2015-01-05 07:55:00 2015-01-05 08:35:00
5 2015-01-05 2015-01-05 09:56:00 2015-01-05 10:44:00
6 2015-01-05 2015-01-05 12:35:00 2015-01-05 13:30:00
7 2015-01-05 2015-01-05 12:36:00 2015-01-05 13:14:00
8 2015-01-05 2015-01-05 14:00:00 2015-01-05 14:45:00
9 2015-01-05 2015-01-05 14:05:00 2015-01-05 14:43:00
10 2015-01-05 2015-01-05 14:10:00 2015-01-05 14:42:00
11 2015-01-05 2015-01-05 16:53:00 2015-01-05 17:23:00
12 2015-01-05 2015-01-05 20:47:00 2015-01-05 21:25:00
13 2015-01-05 2015-01-05 21:54:00 2015-01-05 22:32:00
14 2015-01-05 2015-01-05 22:00:00 2015-01-05 22:45:00
15 2015-01-05 2015-01-05 22:09:00 2015-01-05 22:31:00
16 2015-01-05 2015-01-05 23:23:00 2015-01-05 23:57:00
现在我需要弄清楚如何计算每天重叠事件的数量。我不能包含我在我的问题中发布的循环的输出,因为它在R中没有正确编写。我发布循环从编程的角度来展示我想用循环完成什么,我只是不知道如何在R中创建那种循环。