title: Calculate MAPE on rows by groups
body: <p>I have run an OLS regression on a dataset of housing prices. I've calculated the error for each house versus the predicted values. I have a column in the dataframe with the towns that each house belongs to. I want to calculate the MAPE for each town. My dataframe looks something like this: </p>
  
  <pre><code>HomePr   Error      Town 
  
  1390      0.40093  Clarkvile   
  2010      0.348902 Petersburg  
  2393      0.348902 Petersburg  
  2000      0.348902 Clarkvile  
  7030      0.348902 Pleasant Place  
  4025      0.348902 Petersburg  
  4000      0.348902 Millerstown 
  2086      0.348902 Pleasant Place  
  6058      0.348902 Schneider  
  2000      0.348902 Jebtown 
  </code></pre>
  
  <p>I want to calculate MAPE by <code>Town</code>. So my first step is to get a list of the unique <code>Towns</code> and then calculate MAPE using all the <code>Errors</code> that share that group. Then I want a new column <code>DF$Mape</code> that will give me the MAPE using the houses only within each group of <code>Town</code>. </p>
  
  <p>I am unsure how to go about this. Looking for suggestions.</p>