title: Creating loop to compare rows and create new variable
body: <p>I have a long file that has observations along each row. Here are the variables:</p>
  <ul>
  <li>id = Each unique ID represents a person, and each person has three observations.</li>
  <li>type = Each observation is coded as a type represented by a code (207, 208, 212, 359)</li>
  <li>date = Each observation occurred on a date</li>
  <li>order_num = This represents the sequence in which the observations occurred.</li>
  </ul>
  <p>I want to use a loop to create a new variable that places the type when order_num == 1 in each observation within each individual.</p>
  <p>Here is what I have:</p>
  <pre><code># A tibble: 9 x 4
       id  type date       order_num
    &lt;dbl&gt; &lt;dbl&gt; &lt;chr&gt;          &lt;dbl&gt;
  1     1   212 2020-12-15         1
  2     1   207 2021-01-21         2
  3     1   208 2021-02-21         3
  4     2   207 2020-12-31         1
  5     2   208 2021-01-30         2
  6     2   212 2021-02-28         3
  7     3   208 2021-04-01         1
  8     3   212 2021-05-01         2
  9     3   359 2021-06-01         3
  </code></pre>
  <p>Here is what I want:</p>
  <pre><code>     id  type date       order_num first
    &lt;dbl&gt; &lt;dbl&gt; &lt;chr&gt;          &lt;dbl&gt; &lt;dbl&gt;
  1     1   212 2020-12-15         1   212
  2     1   207 2021-01-21         2   212
  3     1   208 2021-02-21         3   212
  4     2   207 2020-12-31         1   207
  5     2   208 2021-01-30         2   207
  6     2   212 2021-02-28         3   207
  7     3   208 2021-04-01         1   208
  8     3   212 2021-05-01         2   208
  9     3   359 2021-06-01         3   208
  </code></pre>
  <p>As you can see for id 1, the first type that they received in the sequence was 212. The &quot;first&quot; variable now has type 212 in all three of the observations for id 1.</p>
  <p>Here are the data that I'm starting with:</p>
  <pre><code>  ~id,~type,~date,       ~order_num,
    #---|----|------------|-----------|
    1,   212, &quot;2020-12-15&quot;, 1, 
    1,   207, &quot;2021-01-21&quot;, 2, 
    1,   208, &quot;2021-02-21&quot;, 3, 
    2,   207, &quot;2020-12-31&quot;, 1,
    2,   208, &quot;2021-01-30&quot;, 2,
    2,   212, &quot;2021-02-28&quot;, 3,
    3,   208, &quot;2021-04-01&quot;, 1,
    3,   212, &quot;2021-05-01&quot;, 2,
    3,   359, &quot;2021-06-01&quot;, 3)
  </code></pre>
  <p>Unfortunately, when I run the loop that I developed (see below), I cannot get the result that I'm seeking (see below).</p>
  <p>MY CODE:</p>
  <pre><code>  if(df$id[i] == df$id[i-1]){
      df$first &lt;- df$type[i-1]
    } else if (df$id[i] != df$id[i-1]) {
      df$first &lt;- df$type[i]
    } else df$first &lt;- 99
  }
  </code></pre>
  <p>GENERATED BY MY CODE:</p>
  <pre><code># A tibble: 9 x 5
       id  type date       order_num first
    &lt;dbl&gt; &lt;dbl&gt; &lt;chr&gt;          &lt;dbl&gt; &lt;dbl&gt;
  1     1   212 2020-12-15         1   212
  2     1   207 2021-01-21         2   212
  3     1   208 2021-02-21         3   212
  4     2   207 2020-12-31         1   212
  5     2   208 2021-01-30         2   212
  6     2   212 2021-02-28         3   212
  7     3   208 2021-04-01         1   212
  8     3   212 2021-05-01         2   212
  9     3   359 2021-06-01         3   212
  </code></pre>
  <p>I know multiple things wrong because the same type is being assigned to every observation.</p>
  <p>Does anyone have any suggestions for how I could get the result I'm looking for?</p>
  <p>Thanks.</p>