title: Collapsing rows with dplyr
body: <p>I am new to R and am trying to collapse rows based on row values with dplyr. The following example shows the sample data.</p>
  
  <pre><code>set.seed(123)
  
  df&lt;-data.frame(A=c(rep(1:4,4)),
                 B=runif(16,min=0,max=1),
                 C=rnorm(16, mean=1,sd=0.5))
  
     A B          c
  1  1 0.36647435 0.7485365
  2  2 0.51864614 0.8654337
  3  3 0.04596929 0.9858012
  4  4 0.15479619 1.1294208
  5  1 0.76712372 1.2460700
  6  2 0.17666676 0.7402996
  7  3 0.89759874 1.2699954
  8  4 0.90267735 0.7101804
  9  1 0.91744223 0.3451281
  10 2 0.25472599 0.8604743
  11 3 0.10933985 0.8696796
  12 4 0.71656017 1.2648846
  13 1 0.21157810 1.3170205
  14 2 0.14947268 1.2789700
  15 3 0.92251060 1.5696901
  16 4 0.30090579 1.7642853
  </code></pre>
  
  <p>I want to summarize/collapse two rows based on the condition that the rows in column <code>A</code> with values 1 and 2 as one row (as mean of row 1 and 2) . Therefore the final result will have only 12 rows because the other 4 rows has been collapsed.</p>
  
  <p>I tried to use the following <code>dplyr</code> function but to little avail.</p>
  
  <p>install.packages ("tidyverse")
  library (tidyverse)</p>
  
  <pre><code>df %&gt;% summarize_each( fun(i){ for i %in% c(1,2)funs(mean) })
  </code></pre>
  
  <p>The expected output is something like:</p>
  
  <pre><code>   A    B           C
  1  1.5  0.4425602   0.8069851
  3  3    0.04596929  0.9858012
  4  4    0.15479619  1.1294208
  5  1.5  0.4718952   0.9931848
  7  3    0.89759874  1.2699954
  8  4    0.90267735  0.7101804
  9  1.5  0.5860841   0.6028012
  11 3    0.10933985  0.8696796
  12 4    0.71656017  1.2648846
  13 1.5  0.1805254   1.297995
  15 3    0.92251060  1.5696901
  16 4    0.30090579  1.7642853
  </code></pre>
  
  <p>Thank you in advance.</p>