title: Create unique row values in new column based on matching criteria in R
body: <p>I have a dataframe with one identifier column of unique values, and one column which contains specific criteria.</p>
  <p>I want to create a new identifier column of unique values, but where the value also contains information about which criteria it meets. In the example below, I have used <code>case_when()</code> and <code>seq_along()</code> to accomplish this:</p>
  <pre><code>set.seed(1)
  df &lt;- data.frame(
      ID = LETTERS[1:10],
      Criteria = paste0(&quot;Crit &quot;, floor(runif(10, min=1, max=4)))
  )
  df %&gt;%
  mutate(
      ID2 = case_when(
          Criteria == &quot;Crit 1&quot; ~ paste0(&quot;x&quot;, seq_along(Criteria)),
          Criteria == &quot;Crit 2&quot; ~ paste0(&quot;y&quot;, seq_along(Criteria)),
          Criteria == &quot;Crit 3&quot; ~ paste0(&quot;z&quot;, seq_along(Criteria))
      )
  )
  </code></pre>
  <p>Output:</p>
  <pre><code>A data.frame: 10 × 3
  ID  Criteria ID2
  A   c1       x1
  B   c2       y2
  C   c2       y3
  D   c3       z4
  E   c1       x5
  F   c3       z6
  G   c3       z7
  H   c2       y8
  I   c2       y9
  J   c1       x10
  </code></pre>
  <p>The new column, <code>ID2</code>, now has row values that are both unique (numbers 1 to 10) and where the criteria can be identified (letters x, y and z). However, <code>seq_along()</code> inserts a new number for each row regardless of criterion. I'd rather that the count starts anew at one for each criterion. (Eg. for criterion <code>c1</code>: <code>x1</code>, <code>x2</code>, <code>x3</code>, ..., <code>xn</code>; for <code>c2</code>: <code>y1</code>, <code>y2</code>, <code>y3</code>, ..., <code>ym</code>; etc.)</p>
  <p>What I want:</p>
  <pre><code>A data.frame: 10 × 3
  ID  Criteria ID2
  A   c1       x1
  B   c2       y1
  C   c2       y2
  D   c3       z1
  E   c1       x2
  F   c3       z2
  G   c3       z3
  H   c2       y3
  I   c2       y4
  J   c1       x3
  </code></pre>