title: Finding the most recent observation earlier than a certain timestamp with XTS
body: <p>I have an <code>xts</code> object that looks like this:</p>
  
  <pre><code>&gt; q.xts
                                    val
  2011-08-31 09:30:00.002357 -1.0135222
  2011-08-31 09:30:00.003443 -0.2182679
  2011-08-31 09:30:00.005075 -0.5317191
  2011-08-31 09:30:00.009515 -1.0639535
  2011-08-31 09:30:00.011569 -1.2470759
  2011-08-31 09:30:00.012144  0.7678103
  2011-08-31 09:30:00.023813 -0.6303432
  2011-08-31 09:30:00.024107 -0.5105943
  </code></pre>
  
  <p>I calculate a fixed offset from timestamps in another data frame, <code>r</code>. The number of rows in <code>r</code> is significantly fewer than the number of rows in <code>q.xts</code>.</p>
  
  <pre><code>&gt; r
                          time               predict.time
  1 2011-08-31 09:30:00.003443 2011-08-31 09:30:00.002443
  2 2011-08-31 09:30:00.009515 2011-08-31 09:30:00.008515
  3 2011-08-31 09:30:00.024107 2011-08-31 09:30:00.023108
  </code></pre>
  
  <p>The <code>time</code> column corresponds to an observation from <code>q.xts</code> while the <code>predict.time</code> column is 1 millisecond earlier than <code>time</code> (less any precision round offs).</p>
  
  <p>What I would like to do is find the last observation from <code>q.xts</code> that is equal to or earlier in time for each value of <code>predict.time</code>. For the three observations in <code>r</code> above I would expect the following matches:</p>
  
  <pre><code>                        time               predict.time     (time from q.xts)
  1 2011-08-31 09:30:00.003443 2011-08-31 09:30:00.002443  --&gt; 09:30:00.002357
  2 2011-08-31 09:30:00.009515 2011-08-31 09:30:00.008515  --&gt; 09:30:00.005075
  3 2011-08-31 09:30:00.024107 2011-08-31 09:30:00.023108  --&gt; 09:30:00.012144
  </code></pre>
  
  <p>I had approached this by looping over each row in <code>r</code> and performing an <code>xts subset</code>. So, for row 1 of <code>r</code> I would do:</p>
  
  <pre><code>&gt; last(index(q.xts[paste('/', r[1,]$predict.time, sep='')]))
  [1] "2011-08-31 09:30:00.002357 CDT"
  </code></pre>
  
  <p>QUESTION: Doing this with a loop seems clunky and awkward. Is there a better way? I would like to end up with another column in <code>r</code> that provides the exact time or row number for the corresponding value in <code>q.xts</code>.</p>
  
  <hr>
  
  <p><strong>NOTE:</strong> Use this to build the data I've used for this example:</p>
  
  <pre><code>q &lt;- read.csv(tc &lt;- textConnection("
         2011-08-31 09:30:00.002358, -1.01352216
         2011-08-31 09:30:00.003443, -0.21826793
         2011-08-31 09:30:00.005076, -0.53171913
         2011-08-31 09:30:00.009515, -1.06395353
         2011-08-31 09:30:00.011570, -1.24707591
         2011-08-31 09:30:00.012144,  0.76781028
         2011-08-31 09:30:00.023814, -0.63034317
         2011-08-31 09:30:00.024108, -0.51059425"),
       header=FALSE); close(tc)
  colnames(q) &lt;- c('datetime', 'val')
  q.xts &lt;- xts(q[-1], as.POSIXct(q$datetime))
  
  r &lt;- read.csv(tc &lt;- textConnection("
         2011-08-31 09:30:00.003443
         2011-08-31 09:30:00.009515
         2011-08-31 09:30:00.024108"),
       header=FALSE); close(tc)
  colnames(r) &lt;- c('time')
  r$time &lt;- as.POSIXct(strptime(r$time, '%Y-%m-%d %H:%M:%OS'))
  r$predict.time &lt;- r$time - 0.001
  </code></pre>