title: R dplyr: Add column in group_by to count number of males/females
body: <p>I have this dataframe:</p>
  <pre><code>treatment  hh_id hh_size   sex   yob g2000 g2002 g2004 p2000
     &lt;chr&gt;      &lt;dbl&gt;   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
   1 Civic Duty     1       2     1  1941     1     1     1     0
   2 Civic Duty     1       2     1  1947     1     1     1     0
   3 Hawthorne      2       3     1  1951     1     1     1     0
   4 Hawthorne      2       3     1  1950     1     1     1     0
   5 Hawthorne      2       3     1  1982     1     1     1     0
   6 Control        3       3     1  1981     0     0     1     0
   7 Control        3       3     1  1959     1     1     1     0
   8 Control        3       3     1  1956     1     1     1     0
   9 Control        4       2     1  1968     0     0     1     0
  10 Control        4       2     1  1967     1     1     1     0
  </code></pre>
  <p>I want to group it by hh_id &amp; treatment and summarize the rest of the columns by their mean.</p>
  <p>Except, I also want two other columns to count the number of males and females in <strong>each household</strong>, where in the &quot;sex&quot; column <code>female == 1</code> and <code>male == 0</code>.</p>
  <p>Here's what I have so far:</p>
  <pre><code>households &lt;- df %&gt;%
    mutate_if(is.character, factor) %&gt;%
    group_by(hh_id, treatment) %&gt;%
    summarise_if(is.numeric, mean)
  View(households)
  </code></pre>
  <p>which gives me this dataframe:</p>
  <pre><code>   hh_id treatment  hh_size   sex   yob g2000 g2002 g2004 p2000
     &lt;dbl&gt; &lt;fct&gt;        &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
   1     1 Civic Duty       2     1 1944  1     1         1   0  
   2     2 Hawthorne        3     1 1961  1     1         1   0  
   3     3 Control          3     1 1965. 0.667 0.667     1   0  
   4     4 Control          2     1 1968. 0.5   0.5       1   0  
   5     5 Control          1     1 1941  1     1         1   0  
   6     6 Hawthorne        2     1 1947  1     1         1   0  
   7     7 Control          1     1 1969  1     0         1   0  
   8     8 Control          2     1 1964  1     1         1   0.5
   9     9 Self             2     1 1956  0.5   0.5       1   0  
  10    10 Control          1     1 1943  1     1         1   0  
  </code></pre>