title: Mutate function in dplyr not working with Rolling Means/ Moving Averages
body: <p>First and foremost, regardless if you have input or not, thank you for taking your time to view my question. </p>
  
  <p>Let me break down what I am doing, the sample dataset, and the error. </p>
  
  <p>What I currently have is data for several different ID's that list the dispersion per day. (you will see below). I want to loop through the dates and add two columns to the data : <strong>Rolling Means columns &amp; Rolling standard deviation column</strong>. </p>
  
  <p>The code I have written out so far is this:</p>
  
  <pre><code>library(zoo)
  Testing1 &lt;- function(dataset, k) {
  ops &lt;- data.frame()  
  for (i in unique(dataset$Date)) {  
  ops &lt;- dataset %&gt;% mutate(rolling_mean = rollmean(dataset$Dispersion,k)) %&gt;% 
  mutate(rolling_std = rollapply(dataset$Dispersion, width = k, FUN = sd))
  }
  Results &lt;&lt;- ops 
  }
  </code></pre>
  
  <p>however, i get the following error: </p>
  
  <blockquote>
    <p>Error in mutate_impl(.data, dots) : 
      Column <code>rolling_mean</code> must be length 30 (the number of rows) or one, not 26</p>
  </blockquote>
  
  <p>I am assuming that the row differential is due to me specifying a 5 day window for the rolling average, meaning it won't calculate it for the first 4 rows. But how do I go about telling R that it's ok to input NA's on those rows? Or If you guys have any other solution, that would work as well. Please do help.</p>
  
  <p>Heres a sample of the data:</p>
  
  <pre><code>Identifier  Date    Dispersion
  1000    2/15/2018   0.390
  1000    2/16/2018   0.664
  1000    2/17/2018   0.526
  1000    2/18/2018   0.933
  1000    2/19/2018   0.009
  1000    2/20/2018   0.987
  1000    2/21/2018   0.517
  1000    2/22/2018   0.641
  1000    2/23/2018   0.777
  1000    2/24/2018   0.613
  1001    2/15/2018   0.617
  1001    2/16/2018   0.234
  1001    2/17/2018   0.303
  1001    2/18/2018   0.796
  1001    2/19/2018   0.359
  1001    2/20/2018   0.840
  1001    2/21/2018   0.291
  1001    2/22/2018   0.699
  1001    2/23/2018   0.882
  1001    2/24/2018   0.467
  1002    2/15/2018   0.042
  1002    2/16/2018   0.906
  1002    2/17/2018   0.077
  1002    2/18/2018   0.156
  1002    2/19/2018   0.350
  1002    2/20/2018   0.060
  1002    2/21/2018   0.457
  1002    2/22/2018   0.770
  1002    2/23/2018   0.433
  1002    2/24/2018   0.366
  </code></pre>