title: Fast, concise way to generate ordered frequency count of unique matrix rows
body: <p>I have a matrix with some non-unique rows, e.g.:</p>
  
  <pre><code>x &lt;- read.csv(textConnection(
  '0,1,1,0
  0,1,1,0
  1,0,1,0
  0,1,0,1
  1,0,0,1'),
  header = FALSE)
  </code></pre>
  
  <p>The challenge a quick way (a function called <code>umat_count</code> perhaps) to arrive at the count of the unique rows of this matrix, in the order that they appear in x. Ideally the result would look like:</p>
  
  <pre><code>y &lt;- umat_count(x) 
  y
  ##    2    1    1    1 
  </code></pre>
  
  <p>To test this result is what I want, we can create unique version of x and then sample its rows y times and we get back to x:</p>
  
  <pre><code>ux &lt;- unique(x)
  ux[rep(1:nrow(ux), y),]
  
  ## V1 V2 V3 V4
  ## 1    0  1  1  0
  ## 1.1  0  1  1  0
  ## 3    1  0  1  0
  ## 4    0  1  0  1
  ## 5    1  0  0  1
  </code></pre>
  
  <p>So the question is what's a quick way to write <code>umat_count</code>? ATM this is the clunky code I have but I'm sure there's a better way, perhaps a one liner:</p>
  
  <pre><code>umat_count &lt;- function(x) {
    xp &lt;- apply(x, 1, paste0, collapse = "") # "pasted" version of constraints
    freq &lt;- table(xp) # frequency of occurence of each individual
    xu &lt;- unique(x) # save only unique individuals
    rns &lt;- as.integer(row.names(xu)) # save the row names of unique values of ind
    xpu &lt;- xp[rns]
    # xpu &lt;- apply(xu, 1, paste0, collapse = "") # old way of generating ind_pu
    o &lt;- order(xpu, decreasing = TRUE) # the order of the output (to rectify table)
    y &lt;- freq[o] # frequency with which each individual appears (more efficient way?)
    y
  }
  
  y &lt;- umat_count(x)
  </code></pre>
  
  <p>For context, I'm using this in the data preparation stage of an effort to optimise R scripts for 'spatial microsimulation' as described in this course-book: <a href="https://www.dropbox.com/s/ffnrl2ofv18rm3n/book-cambridge.pdf?dl=0" rel="nofollow">https://www.dropbox.com/s/ffnrl2ofv18rm3n/book-cambridge.pdf?dl=0</a></p>
  
  <p>Many thanks.</p>