title: patterns in a deep matrix/dataframe
body: <p>I am trying to find the frequency of patterns in a deep matrix/dataframe (cols: id, variable, value) with 10s of millions of rows.  This is easy to do in a wide matrix as shown below. I was wondering if there was a way to do the same (in a deep matrix) without first converting to wide format.  Thanks.</p>
  
  <pre><code>require(dplyr)
  require(tidyr)
  
  set.seed(100)
  ncol &lt;- 10
  nrow &lt;- 100000
  
  #create sample matrix in wide format
  df1 &lt;- as.data.frame(matrix((runif(nrow*ncol)&gt;0.8) + 0, ncol=ncol))
  cols &lt;- colnames(df1)
  df1 &lt;- filter(df1, rowSums(df1)&gt;0)
  df1 &lt;- cbind(id=seq_len(nrow(df1)), df1)
  
  #compute frequency of patterns
  out1 &lt;- df1 %&gt;%
      group_by_(.dots=cols) %&gt;% summarise(freq=n()) %&gt;% as.data.frame() %&gt;% arrange(desc(freq))
  
  #convert to deep format
  df2 &lt;- df1 %&gt;% 
      gather(variable, value, -id) %&gt;% filter(value&gt;0)
  
  #compute frequency of patterns
  out2 &lt;- df2 %&gt;% spread(variable, value, fill=0) %&gt;% 
      group_by_(.dots=cols) %&gt;% summarise(freq=n()) %&gt;% as.data.frame() %&gt;% arrange(desc(freq))
  
  identical(out1, out2)
  </code></pre>