title: Linear Interpolation using dplyr
body: <p>I'm trying to use the <code>na.approx()</code> function from the <code>zoo</code> library (in conjunction with <code>xts</code>) to interpolate missing values from repeated measures data for multiple individuals with multiple measurements.</p>
  <p>Sample data...</p>
  <pre><code>event.date &lt;- c(&quot;2010-05-25&quot;, &quot;2010-09-10&quot;, &quot;2011-05-13&quot;, &quot;2012-03-28&quot;, &quot;2013-03-07&quot;,    
                  &quot;2014-02-13&quot;, &quot;2010-06-11&quot;, &quot;2010-09-10&quot;, &quot;2011-05-13&quot;, &quot;2012-03-28&quot;,
                  &quot;2013-03-07&quot;, &quot;2014-02-13&quot;)
  variable   &lt;- c(&quot;neck.bmd&quot;, &quot;neck.bmd&quot;, &quot;neck.bmd&quot;, &quot;neck.bmd&quot;, &quot;neck.bmd&quot;, &quot;neck.bmd&quot;,
                  &quot;wbody.bmd&quot;, &quot;wbody.bmd&quot;, &quot;wbody.bmd&quot;, &quot;wbody.bmd&quot;, &quot;wbody.bmd&quot;, &quot;wbody.bmd&quot;)
  value      &lt;- c(0.7490, 0.7615, 0.7900, 0.7730, NA, 0.7420, 1.0520, 1.0665, 1.0760,
                  1.0870, NA, 1.0550)
  ## Bind into a data frame
  df &lt;- data.frame(event.date, variable, value)
  rm(event.date, variable, value)
  ## Convert date
  df$event.date &lt;- as.Date(df$event.date)
  ## Load libraries
  library(magrittr)
  library(xts)
  library(zoo)
  </code></pre>
  <p>I can interpolate one missing data point for a single outcome for a given person using <code>xts()</code> and <code>na.approx()</code>....</p>
  <pre><code>## Subset one variable
  wbody &lt;- subset(df, variable == &quot;wbody.bmd&quot;)
  ## order/index and then interpolate
  xts(wbody$value, wbody$event.date) %&gt;%
    na.approx()
  2010-06-11 1.052000
  2010-09-10 1.066500
  2011-05-13 1.076000
  2012-03-28 1.087000
  2013-03-07 1.070977
  2014-02-13 1.055000
  </code></pre>
  <p>Not ideal having a matrix returned, but I can work around that.  The main problem I have though is that I've multiple outcomes for multiple people.  I, perhaps naively thought that since this is therefore a split-apply-combine problem that I could utilise <code>dplyr</code> to achieve this in the following manner...</p>
  <pre><code>## Load library
  library(dplyr)
  ## group and then arrange the data (to ensure dates are correct)
  df %&gt;%
    group_by(variable) %&gt;%
      arrange(variable, event.date) %&gt;%
        xts(.$value, .$event.date) %&gt;%
          na.approx()
  </code></pre>
  <blockquote>
  <p>Error in xts(., .$value, .$event.date) : order.by requires an appropriate time-based object</p>
  </blockquote>
  <p>It seems that <code>dplyr</code> doesn't play well with <code>xts</code>/<code>zoo</code> and I've spent a couple of hours searching around trying to find tutorials/examples on how to interpolate missing data points in R, but all I've found are single case examples and so far I've been unable to find anything on how to do this for multiple sites for multiple people (I realise I could make it just a multiple people problem by reshaping my data to wide but that still wouldn't solve the problem I'm encountering).</p>
  <p>Any thoughts/advice/insights on how to proceed would be greatly appreciated.</p>
  <p>Thanks</p>
  <p>EDIT : Clarification that some functions come from <code>zoo</code> package.</p>