title: Bin rows and for each bin compute dispersion and return outliers
body: <p>I have a data.frame that is rather large with 12374 rows (genes) and 785 columns (cells). I'd like to group rows according to <code>rowMeans</code> into 20 bins. Within each bin, I'd like to z-normalized the dispersion measure (variance/mean) of all genes within that bin in order to identify outlier genes whose expression values were highly variable even when compared to genes with similar average expression. I would then like to extract out genes which exceed a z-score threshold of 1.7 to identify significantly variable genes from each bin. </p>
  
  <p>My data looks something like this: </p>
  
  <pre><code>&gt; head(temp[,1:5])
                           Cell1                Cell2                 Cell3              Cell4                 Cell5
  0610007P14RIK            0.1439444            0.0000000             0.000000            0.8759335            0.0000000
  0610009B22RIK            0.0000000            0.6776718             0.000000            0.0000000            0.0000000
  0610009O20RIK            0.1439444            0.0000000             0.000000            0.2735741            0.0000000
  0610010B08RIK            1.4769893            1.1369215             1.124842            0.8759335            1.9544187
  0610010F05RIK            0.7944809            0.0000000             0.000000            0.7016789            0.9144108
  0610010K14RIK            0.1439444            0.0000000             1.124842            0.7016789            0.0000000
  </code></pre>
  
  <p>I've attempted to use <code>dplyr</code> to do this but run into errors relating to (what I think to be) the number of bins. This is my attempt:</p>
  
  <pre><code>library(dplyr)
  library(genefilter)
  n_bins = 20
  temp = data
  temp$dispersion = rowMeans(temp)/rowVars(temp)
  outscore = temp %&gt;% mutate(bin=ntile(dispersion,n_bins)) %&gt;% 
    group_by(bin) %&gt;% mutate(zscore=scale(dispersion),outlier=abs(zscore)&gt;1.7)
  </code></pre>
  
  <p>The returned error is <code>rror: dims [product 619] do not match the length of object [618]</code></p>