title: R - get all pairs of values with same ID
body: <p>I'm having trouble manipulating data in the way I need to. Take the following sample data frame:</p>
  
  <pre><code>df &lt;- data.frame(id=factor(c(1,1,1,2,2,3)), person=c('P1','P2','P3','P4','P1','P3'))
  
    id person
  1  1     P1
  2  1     P2
  3  1     P3
  4  2     P4
  5  2     P1
  6  3     P3
  </code></pre>
  
  <p>I want to generate a data frame containing each possible pair of <code>person</code> within each <code>id</code>, <em>all</em> unique arrangements included (i.e., <code>P1</code>-<code>P2</code> and <code>P2</code>-<code>P1</code> are unique). For example:</p>
  
  <pre><code>  id  person1  person2
  1  1       P1       P2
  2  1       P1       P3
  3  1       P2       P1
  4  1       P2       P3
  5  1       P3       P1
  6  1       P3       P2
  7  2       P4       P1
  8  2       P1       P4
  9  3       P3       NA
  </code></pre>
  
  <p>Note: Notice <code>id</code> '3' and <code>person</code> 'P3' does not have any other matching persons, and thus has an NA in the <code>person2</code> column of the final data frame. Though this is highly desired, if it's not possible or incredibly difficult, I will take responses which just omit <code>id</code> 3 or just match the <code>P3</code> value with itself (e.g., <code>P3</code>-<code>P3</code>). </p>
  
  <p>Let me know if I'm not articulating this well, I would be happy to elaborate. Thank you!</p>