title: In R, create a new column based on the order of a 2nd column grouped by a 3rd column
body: <p>This is very similar to some other questions, but I wasn't quite satisfied with the other answers.</p>
  <p>I have data where one column is the outcome of a Latin Square study design, where a participant had three conditions that could have come in six possible orders. I do not have a variable that indicates the order that the participant actually received the study conditions, and so need to create one myself. Here is my current and desired output using a fake example from the first three participants:</p>
  <pre class="lang-r prettyprint-override"><code>library(dplyr)
  #&gt; 
  #&gt; Attaching package: 'dplyr'
  #&gt; The following objects are masked from 'package:stats':
  #&gt; 
  #&gt;     filter, lag
  #&gt; The following objects are masked from 'package:base':
  #&gt; 
  #&gt;     intersect, setdiff, setequal, union
  (current &lt;- tibble(
      participant = c(1,1,1,2,2,2,3,3,3),
      block_code = c(&quot;timed&quot;, &quot;untimed&quot;, &quot;practice&quot;, &quot;untimed&quot;, &quot;practice&quot;, &quot;timed&quot;, &quot;timed&quot;, &quot;untimed&quot;, &quot;practice&quot;)
      ))
  #&gt; # A tibble: 9 × 2
  #&gt;   participant block_code
  #&gt;         &lt;dbl&gt; &lt;chr&gt;     
  #&gt; 1           1 timed     
  #&gt; 2           1 untimed   
  #&gt; 3           1 practice  
  #&gt; 4           2 untimed   
  #&gt; 5           2 practice  
  #&gt; 6           2 timed     
  #&gt; 7           3 timed     
  #&gt; 8           3 untimed   
  #&gt; 9           3 practice
  (desired &lt;- current %&gt;%
      mutate(order_code = c(rep(&quot;tup&quot;, 3), rep(&quot;upt&quot;, 3), rep(&quot;tup&quot;, 3))))
  #&gt; # A tibble: 9 × 3
  #&gt;   participant block_code order_code
  #&gt;         &lt;dbl&gt; &lt;chr&gt;      &lt;chr&gt;     
  #&gt; 1           1 timed      tup       
  #&gt; 2           1 untimed    tup       
  #&gt; 3           1 practice   tup       
  #&gt; 4           2 untimed    upt       
  #&gt; 5           2 practice   upt       
  #&gt; 6           2 timed      upt       
  #&gt; 7           3 timed      tup       
  #&gt; 8           3 untimed    tup       
  #&gt; 9           3 practice   tup
  </code></pre>
  <p><sup>Created on 2022-02-28 by the <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v2.0.1)</sup></p>
  <p>Participants 1 and 3 had the same order, so they ended up with the same code.</p>
  <p><strong>How can I tell R to create a new column based on the order of the <code>block_code</code> variable within a participant?</strong></p>