title: R/tidyr- Creating New Data Frame Rows Based On Column Values
body: <p>Consider the following tibble, which contains meal order data. Variable order_item contains the item ordered. However, sometimes an order contains multiple items, as shown in extra_item1/2.</p>
  <pre><code>order_nbr  &lt;-  c(1, 2, 3, 4, 5)
  order_item &lt;-  c(&quot;Burger&quot;, &quot;Steak&quot;, &quot;Lobster&quot;, &quot;Pasta&quot;, &quot;Salmon&quot;)
  order_date &lt;-  c(&quot;01-01-2022&quot;, &quot;01-02-2022&quot;, &quot;01-03-2022&quot;, &quot;01-04-2022&quot;, &quot;01-05-2022&quot;) 
  extra_item1 &lt;- c(&quot;Fries&quot;, &quot;Fries&quot;, &quot;Roll&quot;, &quot;&quot;, &quot;&quot;)
  extra_item2 &lt;- c(&quot;Shake&quot;, &quot;Wine&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;)
  
  orders &lt;- tibble(order_nbr, order_item, extra_item1, extra_item2)
  </code></pre>
  <p>Here's what the tibble looks like...</p>
  <pre><code># Start with...
  # A tibble: 5 × 5
    order_nbr order_date order_item extra_item1 extra_item2
        &lt;dbl&gt; &lt;chr&gt;      &lt;chr&gt;      &lt;chr&gt;       &lt;chr&gt;      
  1         1 01-01-2022 Burger     &quot;Fries&quot;     &quot;Shake&quot;    
  2         2 01-02-2022 Steak      &quot;Fries&quot;     &quot;Wine&quot;     
  3         3 01-03-2022 Lobster    &quot;Roll&quot;      &quot;&quot;         
  4         4 01-04-2022 Pasta      &quot;&quot;          &quot;&quot;         
  5         5 01-05-2022 Salmon     &quot;&quot;          &quot;&quot;    
  </code></pre>
  <p>It is here that I'd like to process orders somehow such that each extra_item1/2 becomes its own observation in orders.</p>
  <pre><code># Do something...
  </code></pre>
  <p>I know it may be a variation of tidyr's pivot_wider / pivot_longer functions, but I can't quite get there.</p>
  <pre><code># End up with
  # A tibble: 10 × 5
     order_nbr order_date order_item extra_item1 extra_item2
         &lt;dbl&gt; &lt;chr&gt;      &lt;chr&gt;      &lt;chr&gt;       &lt;chr&gt;      
   1         1 01-01-2022 Burger     &quot;&quot;          &quot;&quot;         
   2         2 01-02-2022 Steak      &quot;&quot;          &quot;&quot;         
   3         3 01-03-2022 Lobster    &quot;&quot;          &quot;&quot;         
   4         4 01-04-2022 Pasta      &quot;&quot;          &quot;&quot;         
   5         5 01-05-2022 Salmon     &quot;&quot;          &quot;&quot;         
   6         6 01-01-2022 Fries      &quot;&quot;          &quot;&quot;         
   7         7 01-02-2022 Fries      &quot;&quot;          &quot;&quot;         
   8         8 01-03-2022 Roll       &quot;&quot;          &quot;&quot;         
   9         9 01-01-2022 Shake      &quot;&quot;          &quot;&quot;         
  10        10 01-02-2022 Wine       &quot;&quot;          &quot;&quot;  
  </code></pre>
  <p>Note that the order date of the corresponding extra_item1/2 is maintained with the new rows. The values in extra_item1/2 could be empty strings or NA. Please have a look and make a suggestion. Thanks.</p>