title: Group-specific ID numbers using group_indices or similar in R
body: <p>I am trying to group a series of observations by two columns, and then create a third column with an id number. I've tried <code>group_indices</code>, but that gives each combination of observations a unique number. I want the number to revert to <code>1</code> for the first observation of each group.</p>
  
  <p>In my data there are a series of <code>Sites</code> with a number of rows showing the calendar <code>Day</code> when an observation was collected. I want to calculate the chronological day <em>within</em> a <code>Site</code>.</p>
  
  <pre><code>library(dplyr)
  
  # Make some data
  df &lt;- data.frame(Site = rep(c("A", "B", "C"), each = 70),
              Day = as.integer(rep(c(21,22,23,24,25,26,27,1,2,3,4,5,6,7,
                        24,25,26,27,28,29,30), each = 10)))
  
  # Create Day Number column (this doesn't actually work, but is the sort
  # of thing I'm looking for...)
  df &lt;- df %&gt;% group_by(Site, Day) %&gt;% 
                  mutate(Day.Number = group_indices(Day))
  
  # Desired output
       Site Day Day.Number
  1      A  21     1
  2      A  21     1
  3      A  21     1
  ...
  11     A  22     2
  12     A  22     2
  13     A  22     2
  14     A  22     2
  15     A  22     2
  ...
  141    C  24     1
  142    C  24     1
  143    C  24     1
  144    C  24     1
  ...
  151    C  25     2
  152    C  25     2
  153    C  25     2
  154    C  25     2
  155    C  25     2
  ...
  </code></pre>
  
  <p>This is just a toy dataset to demonstrate the problem. Although most <code>sites</code> will have ten observations of seven <code>days</code> it is not always a given, so I can't just use a sequence of <code>rep()</code> etc.</p>
  
  <p>There is a bit of a discussion about this on github <a href="https://github.com/tidyverse/dplyr/issues/2160" rel="nofollow noreferrer">here</a> and <a href="https://github.com/tidyverse/dplyr/issues/1185" rel="nofollow noreferrer">here</a> but it doesn't seem to have been resolved. Any suggestions for workarounds are much appreciated.</p>