R dplyr - rollmean using group by columns
<p>I have a data frame as below :</p>

<p>(find image if data is not as per format)</p>

<pre><code>Sample Data                     
date    id  name    loc mean    count   mean
9/6/2016    kar1    AAA 100004  0   1   
9/8/2016    kar1    AAA 100004  0   3   
9/9/2016    kar1    AAA 100004  0   4   
9/10/2016   kar1    AAA 100004  0   5   
9/11/2016   kar1    AAA 100004  0   6   
9/12/2016   kar1    AAA 100004  0   7   
9/13/2016   kar1    AAA 100004  0   8   
9/14/2016   kar1    AAA 100004  0   9   
9/7/2016    blr1    BBB 100004  0   2   
</code></pre>

<p>am trying to calculate((7 day rolling mean)) 3 day rolling average (previous 3 day and following 3 day) 
on count field based on id,name,loc but results are not as expected.</p>

<p>find below code:</p>

<pre><code>fnrollmean &lt;- function(x) rollmean(df$count,7,na.pad=TRUE,align="center")

rollmeandf &lt;- df %&gt;% group_by(id,name,loc) %&gt;% arrange(id,name,loc) %&gt;% mutate(funs=fnrollmean(df$count))
</code></pre>

<p>I get error :</p>

<blockquote>
  <p>Error in eval(substitute(expr), envir, enclos) :    incompatible size
  (9), expecting 8 (the group size) or 1</p>
</blockquote>

<p>If I just do :</p>

<pre><code>test2 &lt;- df %&gt;% mutate(funs=fnrollmean(df$count))
</code></pre>

<p>it works but calculates by considering all disease which is wrong.</p>

<p>Please let me know if am missing something or any work around.</p>

<p>Expected results:</p>

<pre><code>date    id  name    loc mean    count   mean
9/6/2016    kar1    AAA 100004  0   1   NA
9/8/2016    kar1    AAA 100004  0   3   NA
9/9/2016    kar1    AAA 100004  0   4   NA
9/10/2016   kar1    AAA 100004  0   5   4.8
9/11/2016   kar1    AAA 100004  0   6   6
9/12/2016   kar1    AAA 100004  0   7   NA
9/13/2016   kar1    AAA 100004  0   8   NA
9/14/2016   kar1    AAA 100004  0   9   NA
9/7/2016    blr1    BBB 100004  0   2   NA
</code></pre>

<p><img src="https://i.stack.imgur.com/6jZv7.jpg" alt="sample data in image"></p>

<p>Thanks</p>