title: Map a list of events (instants) to a list of periods (intervals) in R (with or without lubridate)
body: <p>I have two data frames. One containing time periods marked with <em>character</em> unique IDs and another containing events with another set of unique IDs associated with them</p>
  
  <p>Period DF (code):</p>
  
  <pre><code>periodID &lt;- c("P_UID_00", "P_UID_01", "P_UDI_02", "P_UID_03")
  periodStart &lt;- as.POSIXct(c("2016/02/10 19:00", "2016/02/11 19:00", 
                              "2016/02/12 19:00", "2016/02/13 19:00"))
  periodEnd &lt;- as.POSIXct(c("2016/02/10 21:00", "2016/02/11 21:00", 
                            "2016/02/12 21:00", "2016/02/13 21:00"))
  periodDF &lt;- data.frame(periodID, periodStart, periodEnd)
  </code></pre>
  
  <p>Period DF:</p>
  
  <pre><code>   periodID         periodStart           periodEnd
  1 P_UID_00 2016-02-10 19:00:00 2016-02-10 21:00:00
  2 P_UID_01 2016-02-11 19:00:00 2016-02-11 21:00:00
  3 P_UDI_02 2016-02-12 19:00:00 2016-02-12 21:00:00
  4 P_UID_03 2016-02-13 19:00:00 2016-02-13 21:00:00
  </code></pre>
  
  <p>Event DF (code):</p>
  
  <pre><code>eventID &lt;- c("E_UID_00", "E_UID_01", "E_UDI_02", "E_UID_03")
  eventTime &lt;- as.POSIXct(c("2016/02/09 19:55:01", "2016/02/11 19:12:01", 
                           "2016/02/11 20:22:01", "2016/02/15 19:00:01"))
  eventDF &lt;- data.frame(eventID, eventTime)
  </code></pre>
  
  <p>Event DF:</p>
  
  <pre><code>   eventID           eventTime
  1 E_UID_00 2016-02-09 19:55:01
  2 E_UID_01 2016-02-11 19:12:01
  3 E_UDI_02 2016-02-11 20:22:01
  4 E_UID_03 2016-02-15 19:00:01
  </code></pre>
  
  <p>I want to to map the event times in second DF to the time periods in the first DF in order to match the ID of the event to the ID of the period. Essentially the result table I want to see should look like:</p>
  
  <pre><code>   eventID   periodID
  1 E_UID_00   NA
  2 NA         P_UID_00
  3 E_UID_01   P_UID_01
  4 E_UDI_02   P_UID_01
  5 NA         P_UID_02
  6 NA         P_UID_03
  7 E_UID_03   NA
  </code></pre>
  
  <p>I suppose this can be achieved by using lubricate to transform the start and end cloumns in the first DF to intervals and the use some form of <code>apply</code> and <code>instant %within% interval</code> combination, but I am not really familiar with lubridate and did not manage to produce a working code</p>
  
  <p>Additional considerations:<br>
   - periods are completely arbitrary and can last from seconds to years<br>
   - periods never overlap, so this is not an issue<br>
   - more than one event could be associated with a time period<br>
   - it is possible for DFs to contain unassociatable events and time periods<br>
   - the solution must not include loops<br>
   - does not have to be solved with lubridate, in fact a solution with the base R will be even more welcome.  </p>