title: Identify Two-Way Combinations of Levels in a Column for Each ID
body: <p>I want to identify the two-way combinations of levels in one column grouped by the <code>id</code> and <code>Date</code> variables. Basically, I want the daily unique letter pairs for each person.</p>
  <p>I have a dataframe that looks like this:</p>
  <pre><code>in_df &lt;- data.frame(id = c(1,1,1,1,1,2,2,3), 
                      Date = as.Date(c(&quot;2019-01-01&quot;, &quot;2019-01-01&quot;, &quot;2019-01-01&quot;, &quot;2019-01-02&quot;, &quot;2019-01-02&quot;, &quot;2019-01-01&quot;, &quot;2019-01-01&quot;, &quot;2019-01-01&quot;)), 
                      letter = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;A&quot;, &quot;B&quot;, &quot;A&quot;, &quot;D&quot;, &quot;B&quot;)) 
  
  in_df
    id       Date letter
  1  1 2019-01-01      A
  2  1 2019-01-01      B
  3  1 2019-01-01      C
  4  1 2019-01-02      A
  5  1 2019-01-02      B
  6  2 2019-01-01      A
  7  2 2019-01-01      D
  8  3 2019-01-01      B
  </code></pre>
  <p>And I want one that looks like this:</p>
  <pre><code>out_df
    id       Date letter_1 letter_2
  1  1 2019-01-01        A        B
  2  1 2019-01-01        A        C
  3  1 2019-01-01        B        C
  4  1 2019-01-02        A        B
  5  2 2019-01-01        A        D
  6  3 2019-01-01        B        NA
  </code></pre>
  <p>So the first <code>id</code> and the first <code>Date</code> have letters A, B, and C. I want every unique pair from the three. Order doesn't matter so switching what goes to <code>letter_1</code> and <code>letter_2</code> would be the same thing.</p>
  <p>I have played around with <code>expand.grid</code> and <code>combn</code>, but neither seems quite appropriate for this task.</p>
  <p><strong>EDIT</strong></p>
  <p>I also have cases where there is only one row per <code>id</code>/<code>Date</code> so using combn gives me <code>Error in combn(letter, m = 2) : n &lt; m</code>. How can I add an if case such that the <code>letter_2</code> gets an NA? (I also updated the dfs above to address this)</p>