-1

I have a dataset that is a collection of events (multiple events per day) for an entire year. The variables include date, start time, and end time (among other things). I am trying to find out how many of these events overlap each day. To make my loop simpler, I first sorted my excel file by date and then by start time. I have the general idea of the loop, but cannot figure out how to make it compatible with R.

My idea is to initialize an empty array for counting the time overlaps, loop through each date, loop through each event begin time, check to see if the next event is overlapping, add 1 to my time overlap counter array. This way I can see how many events overlapped with another event each day.

Any help would be extremely appreciated.

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 
      }
    }
  }
  • It would probably help if you included example input and corresponding output. Some guidance is here: http://stackoverflow.com/a/28481250/ – Frank Nov 21 '16 at 22:49

1 Answers1

0

Here is a portion of my dataset:

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

Now I need to figure out how to count the number of overlapping events each day. I can't include output from the loop I posted in my question because it is not written correctly in R. I posted the loop to show from a programming standpoint what I am trying to accomplish with my loop, I just do not know how to go about creating that sort of loop in R.