title: R: Applying a Function to an xts Object
body: <p>I have an xts object:</p>
  
  <pre><code>Anchor_Date &lt;- as.Date("2016-04-19")
  End_Date &lt;- as.Date(Anchor_Date + years(5))
  Number_Days &lt;- End_Date - Anchor_Date  
  Xts_Object &lt;- xts(rep(NA, Number_Days + 1), as.Date(Anchor_Date) + 0:Number_Days)
  </code></pre>
  
  <p>I also have a function that takes the square of time between a date and a start date in years:</p>
  
  <pre><code>Time_Squared_Func &lt;- function(Start_Date, Date) {
    Time_Squared &lt;- as.numeric(((Date - Start_Date) / 365) ^ 2)
    Return (Time_Squared)
  }
  </code></pre>
  
  <p>I want to apply the function to the xts object, using a constant Start_Date = the first day in the series, but have the Date argument be the date in that row of the xts object. The result should be 0 in the first row, gradually increasing to 1 on 2017-04-19, 4 on 2018-04-19 etc...</p>
  
  <p><a href="https://codereview.stackexchange.com/questions/39180/best-way-to-apply-across-an-xts-object">https://codereview.stackexchange.com/questions/39180/best-way-to-apply-across-an-xts-object</a></p>
  
  <p>This suggests that vapply is much faster than apply or lapply, so if possible using vapply would be ideal, but anything working is fine.</p>
  
  <p>Thank you.</p>