title: Change variable values by groups using dplyr
body: <p>My question is I want to change all the missing values to the mean of each group for multiple columns. I want to use <code>dplyr</code> but it does not work for me </p>
  
  <p>For example</p>
  
  <pre><code>iris2 &lt;- iris
  set.seed(1)
  iris2[-5] &lt;- lapply(iris2[-5], function(x) {
    x[sample(length(x), sample(10, 1))] &lt;- NA
    x
  })
  
  impute_missing=function(x){
      x[is.na(x)]=mean(x,na.rm=TRUE)
      return(x)
  }
  
  iris2 %&gt;% groupby (Species) %&gt;% sapply(impute_missing)
  </code></pre>
  
  <p>However the codes did not impute the missing by Species but by the mean of all the non-missing values of each column. Another weird thin is the function was also applied to <code>Species</code> the group variable. Is there any way to impute the mean by species and keep a complete dataframe/</p>