title: How do I pivot_wider() so that duplicates are maintained in their own columns?
body: <p>I have a long data frame that I want to widen. The long data frame includes duplicates and I want to keep them as unique columns in the wide format--but the wide format output combines the duplicates in a single column-list. I've tried a few things with pivot_wider() and thought unnest() might be what I need. Below is the closest that I've been able to get to what I'm looking for, but it's not quite there.</p>
  <p>In the example below, I want the wide data frame to include the values of the variable &quot;surprise&quot; for each &quot;box&quot;--with as many columns as there are unique and non-unique surprises. Conceptually, what I'm after is unique and non-unique surprises in a box.</p>
  <p>Is it possible to widen long data in this way?</p>
  <pre class="lang-r prettyprint-override"><code>library(tidyverse)
  
  # some data; long format
  long_box &lt;- c(&quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;C&quot;, &quot;C&quot;)
  surprise &lt;- c(&quot;apple&quot;, &quot;orange&quot;, &quot;orange&quot;, &quot;apple&quot;, &quot;banana&quot;, &quot;insects&quot;, &quot;apple&quot;, &quot;insects&quot;)
  
  # the data frame I have
  tibble(long_box, surprise)
  
  #&gt; # A tibble: 8 x 2
  #&gt;   long_box surprise
  #&gt;   &lt;chr&gt;    &lt;chr&gt;   
  #&gt; 1 A        apple   
  #&gt; 2 A        orange  
  #&gt; 3 A        orange  
  #&gt; 4 B        apple   
  #&gt; 5 B        banana  
  #&gt; 6 B        insects 
  #&gt; 7 C        apple   
  #&gt; 8 C        insects
  
  # same data, wide format
  wide_box &lt;- c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;)
  a &lt;- c(rep(&quot;apple&quot;,3))
  b &lt;- c(&quot;orange&quot;, &quot;banana&quot;, &quot;insects&quot;)
  c &lt;- c(&quot;orange&quot;, &quot;insects&quot;, NA)
  
  # the data frame format I want
  tibble(wide_box, a, b, c) %&gt;% 
    rename(suprise_1 = a,
           suprise_2 = b,
           suprise_3 = c)
  
  #&gt; # A tibble: 3 x 4
  #&gt;   wide_box suprise_1 suprise_2 suprise_3
  #&gt;   &lt;chr&gt;    &lt;chr&gt;     &lt;chr&gt;     &lt;chr&gt;    
  #&gt; 1 A        apple     orange    orange   
  #&gt; 2 B        apple     banana    insects  
  #&gt; 3 C        apple     insects   &lt;NA&gt;
  
  # this is what I've tried to get from long to wide
  tibble(long_box, surprise) %&gt;% 
    pivot_wider(id_cols = long_box,
                names_from = surprise,
                values_from = surprise)
  
  #&gt; Warning: Values are not uniquely identified; output will contain list-cols.
  #&gt; * Use `values_fn = list` to suppress this warning.
  #&gt; * Use `values_fn = length` to identify where the duplicates arise
  #&gt; * Use `values_fn = {summary_fun}` to summarise duplicates
  #&gt; # A tibble: 3 x 5
  #&gt;   long_box apple     orange    banana    insects  
  #&gt;   &lt;chr&gt;    &lt;list&gt;    &lt;list&gt;    &lt;list&gt;    &lt;list&gt;   
  #&gt; 1 A        &lt;chr [1]&gt; &lt;chr [2]&gt; &lt;NULL&gt;    &lt;NULL&gt;   
  #&gt; 2 B        &lt;chr [1]&gt; &lt;NULL&gt;    &lt;chr [1]&gt; &lt;chr [1]&gt;
  #&gt; 3 C        &lt;chr [1]&gt; &lt;NULL&gt;    &lt;NULL&gt;    &lt;chr [1]&gt;
  </code></pre>
  <p><sup>Created on 2021-07-15 by the <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v2.0.0)</sup></p>