title: R - Unstack a dataframe and create a new dataframe with 2 columns only
body: <p>I have the following goal and having a hard to find other posts here that talk about the same thing.
  My basic code is the following:</p>
  <pre><code>df = data.frame(cusip=c(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot;,&quot;A&quot;,&quot;C&quot;,&quot;Z&quot;,&quot;A&quot;,&quot;B&quot;),
                  port=c(&quot;p1&quot;,&quot;p1&quot;,&quot;p1&quot;,&quot;p1&quot;,&quot;p2&quot;,&quot;p2&quot;,&quot;p2&quot;,&quot;p3&quot;,&quot;p3&quot;))
  </code></pre>
  <p>... do something and get the following dataframe:
  rows = unique values in column 'port'
  column = just one column showing (with a string) all the unique cusip values associated to the portfolio specified in the first column.
  Hence, the result should look like this:</p>
  <pre><code>df2 = data.frame(port=c(&quot;p1&quot;,&quot;p2&quot;,&quot;p3&quot;),
                  cusip=c(&quot;A, B C, D&quot;,    &quot;A, C, Z&quot;,   &quot;A, B&quot;))
  </code></pre>
  <p>I tried to do something like:</p>
  <pre><code>u = unstack(df1)
  </code></pre>
  <p>However, this gives me now a list where every name of the list corresponds to a unique portfolio id and each element of the list contains a vector of undefined length with all the cusip values associated to the portfolio.
  So at this point I'm a little lost.
  I read this solution <a href="https://stackoverflow.com/questions/42514249/converting-a-list-of-lists-of-strings-to-a-data-frame-of-numbers-in-r">Converting a list of lists of strings to a data frame of numbers in R</a> that explains how to convert a list back to dataframe, however it doesn't really help me since the number of cusips associated to any given portfolio is unknown.
  Any suggestions? Thank you</p>