title: How to number/label data-table by group-number from group_by?
body: <p>I have a tbl_df where I want to <code>group_by(u, v)</code> for each distinct integer combination observed with <code>(u, v)</code>.</p>
  <hr />
  <p><strong>EDIT:</strong> this was subsequently resolved by adding the (now-deprecated) <code>group_indices()</code> back in <a href="https://github.com/hadley/dplyr/releases" rel="nofollow noreferrer">dplyr 0.4.0</a></p>
  <hr />
  <p>a) I then want to assign each distinct group some arbitrary distinct number label=1,2,3...
  e.g. the combination (u,v)==(2,3) could get label 1, (1,3) could get 2, and so on.
  How to do this with one <code>mutate()</code>, without a three-step summarize-and-self-join?</p>
  <p>dplyr has a neat function <code>n()</code>, but that gives the number of elements <strong>within</strong> its group, not the overall <strong>number of the group</strong>. <a href="https://cran.r-project.org/web/packages/data.table/data.table.pdf" rel="nofollow noreferrer">In <code>data.table</code> this would simply be called <code>.GRP</code></a>.</p>
  <p>b) Actually what I really want to assign a string/character label ('A','B',...).
  But numbering groups by integers is good-enough, because I can then use <code>integer_to_label(i)</code> as below. Unless there's a clever way to merge these two? But don't sweat this part.</p>
  <pre><code>set.seed(1234)
  
  # Helper fn for mapping integer 1..26 to character label
  integer_to_label &lt;- function(i) { substr(&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;,i,i) }
  
  df &lt;- tibble::as_tibble(data.frame(u=sample.int(3,10,replace=T), v=sample.int(4,10,replace=T)))
  
  # Want to label/number each distinct group of unique (u,v) combinations
  df %&gt;% group_by(u,v) %&gt;% mutate(label = n()) # WRONG: n() is number of element within its group, not overall number of group
  
     u v
  1  2 3
  2  1 3
  3  1 2
  4  2 3
  5  1 2
  6  3 3
  7  1 3
  8  1 2
  9  3 1
  10 3 4
  
  KLUDGE 1: could do df %&gt;% group_by(u,v) %&gt;% summarize(label = n()) , then self-join
  </code></pre>