title: calculating the mean and se of data based on multiple variables in an unbalanced design
body: <p>I am trying to calculate some summary stats on a data set where the units of interest do not appear in every event but it is important to consider every event in calculations such as the mean. I have the following data (t2)</p>
  <pre><code>ca  cat spp eg  len cou
  a1  bd  a   y   62  1
  a1  bd  a   y   63  2
  a1  bd  a   y   66  1
  a1  br  b   y   260 1
  a2  bd  a   y   61  1
  a2  bd  a   y   62  1
  a2  bd  b   y   200 1
  a2  bd  b   y   201 1
  a2  bd  c   y   55  1
  a2  bd  c   y   50  2
  a3  mr  a   n   66  1
  a3  bd  a   n   60  1
  a3  bd  a   n   59  1
  a3  bd  c   n   50  1
  a4  mr  a   n   66  1
  a4  mr  a   n   67  1
  a5  bd  a   n   64  1
  </code></pre>
  <p>I am able to calculate overall means</p>
  <pre><code>t2 %&gt;% 
    mutate(nca=n_distinct(ca)) %&gt;% 
    group_by(spp) %&gt;% 
    summarise(av = sum(cou)/first(nca))
  </code></pre>
  <p>returning</p>
  <pre><code>spp av
  a   2.4         
  b   0.6         
  c   0.8
  </code></pre>
  <p>but I haven't had success when, calculating the mean grouped by levels of eg due to their being a lack of balance between ca and eg. This is what I tried</p>
  <pre><code>t2 %&gt;% 
    group_by(eg) %&gt;% 
    mutate(nca=n_distinct(ca)) %&gt;% 
    group_by(spp, eg) %&gt;% 
    summarise(av = sum(cou)/first(nca))
  </code></pre>
  <p>returning</p>
  <pre><code>spp eg    av
  a   n   2.0000000       
  a   y   3.0000000       
  b   y   1.5000000       
  c   n   0.3333333       
  c   y   1.5000000
  </code></pre>
  <p>obviously only the y levels are correct. Any help to do this succinctly with standard error would be appreciated (preferably using dplyr).</p>
  <p>The correct data would look like</p>
  <pre><code>cat    spp    av    eg
  bd  a   3.0000000   y   
  bd  b   1.0000000   y   
  bd  c   1.5000000   y   
  br  b   0.5000000   y   
  bd  a   1.0000000   n   
  bd  c   0.3333333   n   
  mr  a   1.0000000   n
  </code></pre>
  <p>which I calculated by splitting the data by eg (y or n) calculating the statistics and rejoining</p>
  <pre><code>t2y&lt;- t2 %&gt;% 
    filter(eg%in%&quot;y&quot;) %&gt;%
    mutate(nca=n_distinct(ca)) %&gt;%  
    group_by(cat, spp) %&gt;% 
    summarise(av=sum(cou/nca))%&gt;% 
    mutate(eg=&quot;y&quot;)
  
  t2n&lt;- t2 %&gt;% 
    filter(eg%in%&quot;n&quot;) %&gt;%
    mutate(nca=n_distinct(ca)) %&gt;%  
    group_by(cat, spp) %&gt;% 
    summarise(av=sum(cou/nca)) %&gt;% 
    mutate(eg=&quot;n&quot;)
  
  full_join(t2y, t2n)
  
  </code></pre>
  <p>a more succinct approach would be useful!</p>