title: R - group_by utilizing splinefun
body: <p>I am trying to group my data by Year and CountyID then use splinefun (cubic spline interpolation) on the subset data. I am open to ideas, however the splinefun is a must and cannot be changed.</p>
  
  <p>Here is the code I am trying to use:</p>
  
  <pre><code>age &lt;- seq(from = 0, by = 5, length.out = 18)
  
  TOT_POP &lt;- df %.%
  group_by(unique(df$Year), unique(df$CountyID) %.%
  splinefun(age, c(0, cumsum(df$TOT_POP)), method = "hyman")
  </code></pre>
  
  <p>Here is a sample of my data Year = 2010 : 2013, Agegrp = 1 : 17 and CountyIDs are equal to all counties in the US.  </p>
  
  <pre><code>CountyID    Year        Agegrp      TOT_POP
  1001        2010        1           3586
  1001        2010        2           3952
  1001        2010        3           4282
  1001        2010        4           4136
  1001        2010        5           3154
  </code></pre>
  
  <p>What I am doing is taking the Agegrp 1 : 17 and splitting the grouping into individual years 0 - 84. Right now each group is a representation of 5 years. The splinefun allows me to do this while providing a level of mathematical rigour to the process i.e., splinefun allows me provide a population total per each year of age, in each individual county in the US. </p>
  
  <p>Lastly, the splinefun code by itself does work but within the group_by function it does not, it produces:</p>
  
  <pre><code>Error: wrong result size(4), expected 68 or 1. 
  </code></pre>
  
  <p>The splinefun code the way I am using it works like this</p>
  
  <pre><code>TOT_POP &lt;- splinefun(age, c(0, cumsum(df$TOT_POP)), 
             method  = "hyman") 
  
  TOT_POP = pmax(0, diff(TOT_POP(c(0:85))))
  </code></pre>
  
  <p>Which was tested on one CountyID during one Year. I need to iterate this process over "x" amount of years and roughly 3200 counties. </p>