title: How to lag date-index in a time-series in R?
body: <p>REWRITTEN QUESTION HERE:</p>
  
  <p>I've made some progress but am getting odd behaviour from R...</p>
  
  <p>Here's the xts I'm starting with</p>
  
  <pre><code>&lt;no title&gt;  Value   Value2  Value3
  2002-08-21  21      2       27
  2003-09-10  22      42      87
  2004-02-12  23      62      67
  2005-04-13  24      13      73
  2006-05-13  25      4       28
  2007-08-14  20      68      25
  2008-03-06  19      82      22
  </code></pre>
  
  <p>What I want to produce:</p>
  
  <pre><code> &lt;no title&gt; Value   Value2  Value3  ThisDate    NextDate
      2002-08-21  21      2       27      2002-08-21  2003-09-10
      2003-09-10  22      42      87      2003-09-10  2004-02-12
      2004-02-12  23      62      67      2004-02-12  2005-04-13
      2005-04-13  24      13      73      2005-04-13  2006-05-13
      2006-05-13  25      4       28      2006-05-13  2007-08-14
      2007-08-14  20      68      25      2007-08-14  2008-03-06
      2008-03-06  19      82      22      2008-03-06  NA
  </code></pre>
  
  <p>I've written a function like this:</p>
  
  <pre><code>StackUpAdjacentDates &lt;- function(sourceTimeSeries)
  {
      returnValue &lt;- sourceTimeSeries
  
      thisDate &lt;- as.character(index(sourceTimeSeries))
      nextDate &lt;- c(as.character(thisDate[2:length(thisDate)]),NA)
  
      thisDate &lt;- as.Date(strptime(thisDate, "%Y-%m-%d"))
      nextDate &lt;- as.Date(strptime(nextDate, "%Y-%m-%d"))
  
      # set up thisDate in a new column
      if ("thisDate" %in% colnames(returnValue) )
      {
          returnValue&lt;-returnValue[,-which(colnames(returnValue)=="thisDate")]
      }
      returnValue &lt;- cbind(returnValue, thisDate)
      colnames(returnValue)[ncol(returnValue)] &lt;- "thisDate"
      returnValue$thisDate &lt;- thisDate
  
      # add nextDate in a new column
      if ("nextDate" %in% colnames(returnValue) )
      {
          returnValue&lt;-returnValue[,-which(colnames(returnValue)=="nextDate")]
      }
      returnValue &lt;- cbind(returnValue,nextDate)
      colnames(returnValue)[ncol(returnValue)] &lt;- "nextDate"
      #returnValue$nextDate &lt;- nextDate
  
  }
  </code></pre>
  
  <p>This successfully adds thisDate (running the code step-wise at the command-line).  But the bit that adds nextDate seems to over-write it!  I also seem to get an unexpected row of NAs.  Still working on this...</p>
  
  <pre><code>&lt;no title&gt;  Value   Value2  Value3  nextDate
  2002-08-21  21      78      76      12305
  2003-09-10  22      70      23      12460
  2004-02-12  23      84      22      12886
  2005-04-13  24      97      28      13281
  2006-05-13  25      26      97      13739
  2007-08-14  20      59      22      13944
  2008-03-06  19      64      98      NA
  &lt;NA&gt;        NA      NA      NA      NA
  </code></pre>
  
  <p>I've put "no title" in the first column to indicate that it's the xts date-index rather than actually a part of the vector/matrix.</p>
  
  <p>The bit about removing the extra row is because I've not yet solved the over-write problem and was experimenting.  It doesn't need to be there in the final answer but is where I am up to at present.</p>
  
  <p>And lastly, when I interrogate this result and try to convert nextDate to a date I get....</p>
  
  <pre><code>&gt; as.Date(returnValue$nextDate)
  Error in as.Date.default(returnValue$nextDate) : 
    do not know how to convert 'returnValue$nextDate' to class "Date"
  </code></pre>
  
  <p>So I'm in a bit of a muddle...</p>
  
  <p>ORIGINAL QUESTION BELOW:</p>
  
  <p>I have a time-series in R (which I am learning fast, but clearly not fast enough!) like this</p>
  
  <pre><code>             Value
  2002-08-21    21
  2003-09-10    22
  2004-02-12    23
  2005-04-13    24
  2006-05-13    25
  2007-08-14    20
  2008-03-06    19
  </code></pre>
  
  <p>I want to create a derivative of it with the date-index in the NEXT row in a new column in each row:</p>
  
  <pre><code>              Value    NextDate
  2002-08-21    21       2003-09-10
  2003-09-10    22       2004-02-12
  2004-02-12    23       2005-04-13
  2005-04-13    24       2006-05-13
  2006-05-13    25       2007-08-14
  2007-08-14    20       2008-03-06
  2008-03-06    19       [...]
  </code></pre>
  
  <p>It's pretty easy to do for Value (using Lag) but not for the date-index iteself.</p>
  
  <p>I can probably work out how to do it using various lookups and the like, but it is messy.  You have to match on some other field, or fiddle around with row-numbers which doesn't feel very "true to R".</p>
  
  <p>Is there a nice, neat, elegant way to do it?</p>
  
  <p>I'm pretty sure I'll go "D'OH!" as soon as someone gives the answer!  But so far I haven't found an answer on this site for lagging the date-index.</p>
  
  <p>The reason I want to do this is I then want to use each pair of dates in a row to interrogate another series.  So there might be a better way to do this.</p>