title: subset data based on a date time difference or window
body: <p>I have location data that has a date time stamp.  These locations were supposed to be collected at regular time intervals, however it doesn't always happen that way.  I need to extract those locations that are within a time window.  So, for instance, locations that are 12 hours apart.  If I start with the Datetime from location 1, find the next location that is 12 hours later in time. If there isn't one exactly 12 hours, then the next closest to that new specified time.  Then take that new location and find the next within 12h from that time.  And I have to do this for each unique ID. </p>
  
  <pre><code>COLLAR_ID                    dt
  2159    2006-01-27 13:02:55
  2159    2006-01-27 14:01:12
  2159    2006-01-27 15:01:04
  2159    2006-01-27 16:01:09
  </code></pre>
  
  <p>is what the data looks like and here's a short subset of the data that you can cut and paste.  Note it is all the same ID, I have 5 different IDs with different start date/times</p>
  
  <pre><code>structure(list(COLLAR_ID = c(2159L, 2159L, 2159L, 2159L, 2159L, 
  2159L, 2159L, 2159L, 2159L, 2159L, 2159L, 2159L, 2159L, 2159L, 
  2159L, 2159L, 2159L, 2159L, 2159L, 2159L), dt = structure(c(1138366975, 
  1138370472, 1138374064, 1138377669, 1138381264, 1138384873, 1138388503, 
  1138399312, 1138402842, 1138406507, 1138413700, 1138417261, 1138420848, 
  1138424444, 1138428071, 1138431695, 1138435287, 1138438938, 1138442428, 
  1138446098), class = c("POSIXct", "POSIXt"), tzone = "GMT")), .Names = c("COLLAR_ID", 
  "dt"), class = "data.frame", row.names = c(NA, 20L))
  </code></pre>
  
  <p>So I think that from the example data, If my start date is 2006-01-27 at 00:00:00 time, then the next location it should record is at 12:00:00 - however this location does not exist so it should record 13:02:55.  But even this is "out side" a strict 1 hour buffered window by 2 minutes.   </p>
  
  <p>I had thought of converting the date time to a Julian decimal number to make it easier to work with, but I don't know how to do that.  Rounding the date/time to just hours would be OK, except sometimes there are 2 or 3 locations taken within a 1 hour time interval, so then I somehow need to select among those 1 location that is "closest in time" to the original start.</p>
  
  <p>So to add new detail likely making things more confusing - some data initially was collected at 1 hour intervals, then after 3 weeks it switched to 12 hours.  But, I do not know the programmed time each one was supposed to switch.  Others started at 12 hours with a start of 00:00:00, but switched to 1 hour intervals and then after several days switched to 12 hour - but again without knowing which time of day it made the switch.  so, it could have switched to the 12 hour starting at 2pm.  </p>
  
  <p>I was trying to look at <a href="https://stackoverflow.com/questions/18092081/r-subset-a-data-frame-based-on-times-that-are-within-a-certain-number-of-minute">this stack overflow conversation</a> but can't see how that would work.  So, this is my attempt below, which I have updated now from original posting of question.  It DOES NOT WORK.  I am still working on it....It still seems rather clunky of code.</p>
  
  <pre><code> test2 = test2[order(test2$COLLAR_ID,test2$dt),]
  test2$dt &lt;- as.POSIXct(strptime((test2$dt), "%Y-%m-%d %H:%M:%S"), tz="GMT")
  MinInterval = 12 #minimum time interval (in hours) between consecutive locations
  row = 0           # Keeps track of row within alldata
  Endtest2 = 2                  #keeps track of row within individual within all data
  SubData1 = test2[1,]
  IDNames = levels(as.factor(test2$COLLAR_ID))
  test22 = data.frame()
  
  for (n in 1:length(IDNames)){
    IndivData = test2[test2$COLLAR_ID==IDNames[n],]
    row = row+1               #Continues to track next row between individuals
    Endtest2 = 2               #restarts counting the rows for NEXT individual
    SubData1[row,]=IndivData[1,]
  
    while (Endtest2&lt;nrow(IndivData) ){
      timediff = difftime(IndivData$dt[Endtest2],SubData1$dt[row],units = "hours")
  
      if (timediff&gt;MinInterval){          #If time difference is greater than 47 hours then do
        row = row+1
        SubData1=rbind(SubData1,IndivData[Endtest2,])
        Endtest2 = Endtest2+1                
      } else{
        Endtest2 = Endtest2+1
      }
    } #end while loop
  
  } #end loop through individuals
  test22 =SubData1
  } #end conditional to subset data
  </code></pre>
  
  <p>I apologize and am embarrassed to say, that I completely forgot I <a href="https://stackoverflow.com/questions/6448068/irregular-time-series-data-can-i-make-it-regular-in-r?rq=1">posted a question</a> on this (using similar code) long back, but never got any solutions.  I had abandoned the entire endeavor and yet now am revisiting it with new data (much more chaotic data) and a new need.  The script does not filter out the right data.</p>