title: summarizing counts of a factor with dplyr
body: <p>I want to group a data frame by a column (owner) and output a new data frame that has counts of each type of a factor at each observation. The real data frame is fairly large, and there are 10 different factors.</p>
  
  <p>Here is some example input:</p>
  
  <pre><code>library(dplyr)
  df = tbl_df(data.frame(owner=c(0,0,1,1), obs1=c("quiet", "loud", "quiet", "loud"), obs2=c("loud", "loud", "quiet", "quiet")))
  
    owner  obs1  obs2
  1     0 quiet  loud
  2     0  loud  loud
  3     1 quiet quiet
  4     1  loud quiet
  </code></pre>
  
  <p>I was looking for output that looks like this:</p>
  
  <pre><code>out = data.frame(owner=c("0", "0", "1", "1"), observation=c("obs1", "obs2", "obs1", "obs2"), quiet=c(1, 0, 1, 2), loud=c(1, 2, 1, 0))
  
    owner observation quiet loud
  1     0        obs1     1    1
  2     0        obs2     0    2
  3     1        obs1     1    1
  4     1        obs2     2    0
  </code></pre>
  
  <p>Melting gets me partway there:</p>
  
  <pre><code>melted = tbl_df(melt(df, id=c("owner")))
  
    owner variable value
  1     0     obs1 quiet
  2     0     obs1  loud
  3     1     obs1 quiet
  4     1     obs1  loud
  5     0     obs2  loud
  6     0     obs2  loud
  7     1     obs2 quiet
  8     1     obs2 quiet
  </code></pre>
  
  <p>But what's the last step? If 'value' was a numeric, I'd just go:</p>
  
  <pre><code>melted %&gt;% group_by(owner, variable) %&gt;% summarise(counts=sum(value))
  </code></pre>
  
  <p>Thanks so much!</p>