title: Given start and end times, create hourly labels to indicate whether an hour is in the duration or not
body: <p>I have start and end times of some commercial event for a couple of locations. The event may or may not take place on each day and the event duration does not overlap. For example, run this:  </p>
  
  <pre><code>inputdata = data.frame(
          location = c('x','x','y','z','z'),
          start = c(as.POSIXct("2010/1/1 8:28:00"),as.POSIXct("2010/1/2 7:20:00"),
                    as.POSIXct("2010/1/1 10:22:00"),
                    as.POSIXct("2010/1/5 13:28:00"),as.POSIXct("2010/1/7 15:39:00")),
          end = c(as.POSIXct("2010/1/1 13:25:00"),as.POSIXct("2010/1/2 10:09:00"),
                  as.POSIXct("2010/1/1 15:24:00"),
                  as.POSIXct("2010/1/6 00:28:00"),as.POSIXct("2010/1/7 19:34:00"))
  )
  </code></pre>
  
  <p>The input data looks like:</p>
  
  <pre><code>  location               start                 end
  1        x 2010-01-01 08:28:00 2010-01-01 13:25:00
  2        x 2010-01-02 07:20:00 2010-01-02 10:09:00
  3        y 2010-01-01 10:22:00 2010-01-01 15:24:00
  4        z 2010-01-05 13:28:00 2010-01-06 00:28:00
  5        z 2010-01-07 15:39:00 2010-01-07 19:34:00
  </code></pre>
  
  <p>I want to construct an hourly dataset with three columns: 1.location, 2.hour, and 3.indicator and each row is for a pair of location and sharp hour (for instance, <code>as.POSIXct("2010/1/1 13:00:00")</code>) where indicator is a dummy, =1 if this hour is between some event start and end times for the location.
  For instance, let's say the output hourly data are for 2010-01-01 to 2010-01-07. Run this:</p>
  
  <pre><code>output = data.frame(
  location = rep(c('x','y','z'),
  each=length(seq(as.POSIXct("2010/1/1"), as.POSIXct("2010/1/7 23:00:00"), "hours"))),
  hour = rep(seq(as.POSIXct("2010/1/1"), as.POSIXct("2010/1/7 23:00:00"), "hours"),3),
  indicator = rep(0,3*length(seq(as.POSIXct("2010/1/1"), as.POSIXct("2010/1/7 23:00:00"), "hours"))))
  </code></pre>
  
  <p>So we get the first six rows look like this: </p>
  
  <pre><code>  location                hour indicator
  1        x 2010-01-01 00:00:00         0
  2        x 2010-01-01 01:00:00         0
  3        x 2010-01-01 02:00:00         0
  4        x 2010-01-01 03:00:00         0
  5        x 2010-01-01 04:00:00         0
  6        x 2010-01-01 05:00:00         0
  </code></pre>
  
  <p>Now, we need to change the value of indicator to 1 if the hour in the same row has an event in effect for the location in the same row.
  For instance, since location x has an event between 8:28 am on 2010/1/1 and 13:25 pm on 2010/1/1. So the rows for 7 am to 14 pm should look like this:</p>
  
  <pre><code>  location                hour indicator
  8        x 2010-01-01 07:00:00         0
  9        x 2010-01-01 08:00:00         1
  10       x 2010-01-01 09:00:00         1
  11       x 2010-01-01 10:00:00         1
  12       x 2010-01-01 11:00:00         1
  13       x 2010-01-01 12:00:00         1
  14       x 2010-01-01 13:00:00         1
  15       x 2010-01-01 14:00:00         0
  </code></pre>
  
  <p>It seems that I can do exhaustively search for each pair of location and hour and update the value of indicator is the hour is between the start and end hour of some event at that location. But I doubt this is the best way.</p>
  
  <p>Or I am thinking that I can first, convert the input data to hourly data where the hour would be there only if they are between the start and end hour. In other words, the converted data should look like:</p>
  
  <pre><code> location                hour indicator
  1       x 2010-01-01 08:00:00         1
  2       x 2010-01-01 09:00:00         1
  3       x 2010-01-01 10:00:00         1
  4       x 2010-01-01 11:00:00         1
  5       x 2010-01-01 12:00:00         1
  6       x 2010-01-01 13:00:00         1
  7       x 2010-01-02 07:00:00         1
  8       x 2010-01-02 08:00:00         1
  9       x 2010-01-02 09:00:00         1
  10      x 2010-01-02 10:00:00         1
  11      y 2010-01-01 10:00:00         1
  12      y 2010-01-01 11:00:00         1
  </code></pre>
  
  <p>and then I go from there to get the correct indicators for each hour for each location. Though, I don't know how to convert the start/end hours to hourly observations.</p>
  
  <p>This is all I get for this problem so far. </p>
  
  <p>With this said, I do not have a solution and would like to ask for help.</p>
  
  <p>Also, all I want is that output with three columns. When contributing, please do not constrained by my thoughts which may not be efficient.</p>
  
  <p>It is worth mentioning that the actual problem covers 5 years and there are 30 locations. So the algorithm needs to be efficient.</p>