title: Match data to nearest time value by id
body: <p>I have generated a series of hourly time stamps with:</p>
  
  <pre><code>intervals &lt;- seq(as.POSIXct("2018-01-20 00:00:00", tz = 'America/Los_Angeles'), as.POSIXct("2018-01-20 03:00:00", tz = 'America/Los_Angeles'), by="hour")
  
  &gt; intervals
  [1] "2018-01-20 00:00:00 PST" "2018-01-20 01:00:00 PST" "2018-01-20 02:00:00 PST"
  [4] "2018-01-20 03:00:00 PST" 
  </code></pre>
  
  <p>Given a dataset with messy and unevenly spaced timestamps, how would one match time values from that dataset to the closest hourly timestamp by <code>id</code>, and remove other timestamps in between?  For example:</p>
  
  <pre><code>&gt; test
                           time      id     amount
  312   2018-01-20 00:02:14 PST       1 54.9508346
  8652  2018-01-20 00:54:41 PST       2 30.5557992
  13809 2018-01-20 01:19:27 PST       3 90.5459248
  586   2018-01-20 00:03:35 PST       1 79.7635973
  9077  2018-01-20 00:56:37 PST       2 75.5356406
  21546 2018-01-20 02:25:05 PST       3 36.6017705
  7275  2018-01-20 00:47:45 PST       1 12.7618139
  12768 2018-01-20 01:15:30 PST       2 72.4465838
  1172  2018-01-20 00:08:01 PST       3 81.0468155
  24106 2018-01-20 03:04:10 PST       1  0.8615881
  14464 2018-01-20 01:25:04 PST       2 49.8718743
  15344 2018-01-20 01:29:30 PST       3 85.0054113
  14255 2018-01-20 01:23:22 PST       1 34.5093891
  21565 2018-01-20 02:25:40 PST       2 69.0175725
  15602 2018-01-20 01:31:32 PST       3 61.8602426
  </code></pre>
  
  <p>Would produce:</p>
  
  <pre><code>&gt; output
               interval id     amount
  1 2018-01-20 01:00:00  1 12.7618139
  2          2018-01-20  1 54.9508346
  3 2018-01-20 03:00:00  1  0.8615881
  4 2018-01-20 01:00:00  2 75.5356400
  5 2018-01-20 02:00:00  2 69.0175700
  6          2018-01-20  3 81.0468200
  7 2018-01-20 01:00:00  3 90.5459200
  8 2018-01-20 02:00:00  3 36.6017700
  </code></pre>
  
  <p>I understand that there exists a possible solution in <code>data.table</code></p>
  
  <pre><code>setDT(reference)[data, refvalue, roll = "nearest", on = "datetime"]
  </code></pre>
  
  <p>with <code>roll = nearest</code>, but how would one keep find the nearest match in <code>intervals</code> for every <code>id</code> in <code>test</code> and retain the <code>amount</code> attribute ? </p>
  
  <p>Any suggestions would be appreciated! Here is the sample data: </p>
  
  <pre><code> dput(test)
  structure(list(time = c("2018-01-20 00:02:14 PST", "2018-01-20 00:54:41 PST", 
  "2018-01-20 01:19:27 PST", "2018-01-20 00:03:35 PST", "2018-01-20 00:56:37 PST", 
  "2018-01-20 02:25:05 PST", "2018-01-20 00:47:45 PST", "2018-01-20 01:15:30 PST", 
  "2018-01-20 00:08:01 PST", "2018-01-20 03:04:10 PST", "2018-01-20 01:25:04 PST", 
  "2018-01-20 01:29:30 PST", "2018-01-20 01:23:22 PST", "2018-01-20 02:25:40 PST", 
  "2018-01-20 01:31:32 PST"), id = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 
  1, 2, 3, 1, 2, 3), amount = c(54.9508346011862, 30.5557992309332, 
  90.5459248460829, 79.763597343117, 75.5356406327337, 36.6017704829574, 
  12.7618139144033, 72.4465838400647, 81.0468154959381, 0.861588073894382, 
  49.8718742514029, 85.0054113194346, 34.5093891490251, 69.0175724914297, 
  61.8602426256984)), .Names = c("time", "id", "amount"), row.names = c(312L, 
  8652L, 13809L, 586L, 9077L, 21546L, 7275L, 12768L, 1172L, 24106L, 
  14464L, 15344L, 14255L, 21565L, 15602L), class = "data.frame")
  </code></pre>