title: Using dplyr summarise_each() with is.na()
body: <p>I'm trying to wrap some dplyr magic inside a function to produce a data.frame that I then print with xtable.</p>
  
  <p>The ultimate aim is to have a dplyr version of <a href="http://gettinggeneticsdone.blogspot.co.uk/2011/02/summarize-missing-data-for-all.html" rel="noreferrer">this</a> working, and reading around I came across the very useful <code>summarise_each()</code> function which after subsetting with <code>regroup()</code> (since this is within a function) I can then use to get all columns parsed.</p>
  
  <p>The problem I've encountered (so far) is with calling <code>is.na()</code> from within <code>summarise_each(funs(is.na))</code> as I'm told <code>Error: expecting a single value</code>.</p>
  
  <p>I'm purposefully <em>not</em> posting my function just yet but a minimal example follows (NB - This uses <code>group_by()</code> whilst in my function I replace this with <code>regroup()</code>)...</p>
  
  <pre><code>library(dplyr)
  library(magrittr)
  &gt; t &lt;- data.frame(grp = rbinom(10, 1, 0.5),
                  a = as.factor(round(rnorm(10))),
                  b = rnorm(10),
                  c = rnorm(10))
  t %&gt;%
  group_by(grp) %&gt;%  ## This is replaced with regroup() in my function
  summarise_each(funs(is.na))
  Error: expecting a single value
  </code></pre>
  
  <p>Running this fails, and its the call to <code>is.na()</code> that is the problem since if I instead work out the number of observations in each (required to derive the proportion of missing) it works...</p>
  
  <pre><code>&gt; t %&gt;%
  group_by(grp) %&gt;%  ## This is replaced with regroup() in my function
  summarise_each(funs(length))
  Source: local data frame [2 x 4]
  
    grp a b c
  1   0 8 8 8
  2   1 2 2 2
  </code></pre>
  
  <p>The real problem though is that I do not need just <code>is.na()</code> within each column, but the <code>sum(is.na())</code> as per the linked example so what I really would like is...</p>
  
  <pre><code>&gt; t %&gt;%
  group_by(grp) %&gt;%  ## This is replaced with regroup() in my function
  summarise_each(funs(propmiss = sum(is.na) / length))
  </code></pre>
  
  <p>But the problem is that <code>sum(is.na)</code> doesn't work as I expect it to (likely because my expectation is wrong!)...</p>
  
  <pre><code>&gt; t %&gt;%
  group_by(grp) %&gt;%  ## This is replaced with regroup() in my function
  summarise_each(funs(nmiss = sum(is.na)))
  Error in sum(.Primitive("is.na")) : invalid 'type' (builtin) of argument
  </code></pre>
  
  <p>I tried calling <code>is.na()</code> explicitly with the brackets but that too returns an error...</p>
  
  <pre><code>&gt; t %&gt;%
  + group_by(grp) %&gt;%  ## This is replaced with regroup() in my function
  + summarise_each(funs(nmiss      = sum(is.na())))
  Error in is.na() : 0 arguments passed to 'is.na' which requires 1
  </code></pre>
  
  <p>Any advice or pointers to documentation would be very gratefully received.</p>
  
  <p>Thanks,</p>
  
  <p>slackline</p>