title: Increase efficiency of dplyr summarising
body: <p>I am trying to sort and make a new table from a large data set (>60k; NDw) with a sample here</p>
  
  <pre><code>       Season     ENo      HNo Month Day Year Group
  638447   2011 A903851  1881023    10   6 2011 Ducks
  589219   2010 C409324  3648019    10   8 2010 Ducks
  137451   2006 M576033  2506116    10  13 2006 Ducks
  883040   2013 P886755 43313010    10  17 2013 Ducks
  851378   2013 C700399 36413199    11   5 2013 Geese
  552791   2010 M902312  2508141    11  16 2010 Ducks
  152368   2006 M599973  2496101    10   3 2006 Ducks
  395393   2008 C412049  3646096    10  28 2008 Ducks
  857709   2013 C671619 36413012     9  15 2013 Ducks
  67354    2005 C349762  3643011    10  22 2005 Geese
  67126    2005 C427496  3643037    11  25 2005 Geese
  62260    2005 C349776  3643023    10   7 2005 Ducks
  847364   2013 C570491 36411001    10   5 2013 Ducks
  447414   2009 A686943  1808206    11   3 2009 Geese
  474743   2009 M813353  2509214    10  24 2009 Ducks
  439477   2009 A746048  1639142    10  26 2009 Ducks
  781218   2012 P792862  4142177    11  27 2012 Geese
  806946   2013 M052893 20712036    11   5 2013 Ducks
  174932   2006 C450351  3645098    12   5 2006 Geese
  828816   2013 M054683 25012010     9  30 2013 Ducks
  </code></pre>
  
  <p>I want to group by Season and HNo and get a number of new variables. These include how many groups each Season/HNo is in, a count of rows total, in each group, and each group during each month. The result would look like this, but with all months.</p>
  
  <pre><code>  Season     HNo groupN total.envelopes ducks geese Octducks
  1   2005 1253041      1               2     2     0        2
  2   2005 1254026      1               5     5     0        5
  3   2005 1254063      2              26    23     3        0
  4   2005 1254115      2              14    10     4       10
  5   2005 1274023      2              39    28    11       28
  </code></pre>
  
  <p>I have code that works but it runs slow and I feel like there should be a better way to code this block. Maybe I'm wrong, and it's not a large issue, just wanted to learn how to make my code more efficient. Here is what I use to get the above output.</p>
  
  <pre><code>NDw1 = NDw %&gt;%
    group_by(Season,HNo) %&gt;%
    summarise(groupN = n_distinct(Group),
              total.envelopes=n(),
              ducks = length(ENo[Group %in% 'Ducks']),
              geese = length(ENo[Group %in% 'Geese']),
              Octducks = length(ENo[Group=='Ducks' &amp; Month == 10]))
  </code></pre>
  
  <p>The entire code has lines for Aug-Jan ducks and geese. I tried to use count rather than length but it didn't work with a factor variable as is ENo. Any thoughts would be appreciated. Thanks for  your time and help.</p>