title: Within group_by, mutate a new column that grabs values of a column based on their order of appearance
body: <p>I'm wrangling a dataset that has a cross-over trial design. Here is a toy example with similar structure:</p>
  <pre class="lang-r prettyprint-override"><code>df &lt;- structure(list(subject = c(&quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;b&quot;, 
  &quot;b&quot;, &quot;b&quot;, &quot;b&quot;, &quot;c&quot;, &quot;c&quot;, &quot;c&quot;, &quot;c&quot;, &quot;c&quot;, &quot;c&quot;), treatment = c(&quot;none&quot;, 
  &quot;placebo&quot;, &quot;placebo&quot;, &quot;drug&quot;, &quot;drug&quot;, &quot;drug&quot;, &quot;none&quot;, &quot;drug&quot;, 
  &quot;placebo&quot;, &quot;placebo&quot;, &quot;none&quot;, &quot;placebo&quot;, &quot;drug&quot;, &quot;drug&quot;, &quot;drug&quot;, 
  &quot;drug&quot;), day = c(0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 1, 2, 3, 4, 
  5)), row.names = c(NA, -16L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;
  ))
  </code></pre>
  <pre class="lang-r prettyprint-override"><code># A tibble: 16 × 3
     subject treatment   day
     &lt;chr&gt;   &lt;chr&gt;     &lt;dbl&gt;
   1 a       none          0
   2 a       placebo       1
   3 a       placebo       2
   4 a       drug          3
   5 a       drug          4
   6 a       drug          5
   7 b       none          0
   8 b       drug          1
   9 b       placebo       2
  10 b       placebo       3
  11 c       none          0
  12 c       placebo       1
  13 c       drug          2
  14 c       drug          3
  15 c       drug          4
  16 c       drug          5
  </code></pre>
  <p>So, every subject starts with a &quot;none&quot; value in <code>treatment</code>, and then has a few days of either <code>placebo</code> or a <code>drug</code> treatment, followed by a few days of the other.
  What I want is a new <code>stage</code> column that tells me the chronological stage of the experiment based on the order of <code>treatment</code> value. In other words, the starting <code>none</code> value in a subject will always be the &quot;first&quot; stage of the experiment, the next chronologically appearing value in <code>treatment</code> within that subject will be &quot;second&quot; stage, and the last appearing value will be the &quot;third&quot; stage.</p>
  <p>So the output I want would look like this:</p>
  <pre class="lang-r prettyprint-override"><code># A tibble: 16 × 4
     subject treatment   day stage 
     &lt;chr&gt;   &lt;chr&gt;     &lt;dbl&gt; &lt;chr&gt; 
   1 a       none          0 first 
   2 a       placebo       1 second
   3 a       placebo       2 second
   4 a       drug          3 third 
   5 a       drug          4 third 
   6 a       drug          5 third 
   7 b       none          0 first 
   8 b       drug          1 second
   9 b       placebo       2 third 
  10 b       placebo       3 third 
  11 c       none          0 first 
  12 c       placebo       1 second
  13 c       drug          2 third 
  14 c       drug          3 third 
  15 c       drug          4 third 
  16 c       drug          5 third  
  </code></pre>
  <p>What made sense to me was using a combination of <code>group_by</code> and <code>mutate</code> with a <code>factor</code> of <code>treatment</code>, which doesn't work</p>
  <pre class="lang-r prettyprint-override"><code>#my failed attempt
  df %&gt;% 
    arrange(subject, day) %&gt;% #needed for my actual dataset
    group_by(subject) %&gt;% 
    mutate(stage=factor(treatment, levels=c(&quot;first&quot;, &quot;second&quot;, &quot;third&quot;))) %&gt;% 
    ungroup(
  </code></pre>
  <p>which gives:</p>
  <pre class="lang-r prettyprint-override"><code># A tibble: 16 × 4
     subject treatment   day stage 
     &lt;chr&gt;   &lt;chr&gt;     &lt;dbl&gt; &lt;fct&gt; 
   1 a       none          0 second
   2 a       placebo       1 third 
   3 a       placebo       2 third 
   4 a       drug          3 first 
   5 a       drug          4 first 
   6 a       drug          5 first 
   7 b       none          0 second
   8 b       drug          1 first 
   9 b       placebo       2 third 
  10 b       placebo       3 third 
  11 c       none          0 second
  12 c       placebo       1 third 
  13 c       drug          2 first 
  14 c       drug          3 first 
  15 c       drug          4 first 
  16 c       drug          5 first
  </code></pre>
  <p>The problem is that the labels are being shown based on the alphabetic order of the &quot;treatment&quot; values, but I want them to appear in the order of appearance of the <code>treatment</code> values within each subject. I also tried with <code>levels</code> instead of <code>labels</code> and I just get all <code>NA</code>s.</p>
  <p>Any help would be much appreciated. A <code>dplyr</code> solution is preferred but would happily work with any other.</p>