title: r - Populate matrix with count data
body: <p>Assume I have the following data:</p>
  
  <pre><code>df &lt;- data.frame(teams=c("A", "B", "C", "D"), 
              players=c(5,2,1,2), 
              stringsAsFactors = FALSE)
  
  &gt; df
    teams players
  1     A       5
  2     B       2
  3     C       1
  4     D       2
  </code></pre>
  
  <p>I want to fill a weighted 4x4 matrix that resembles the pairwise combinations of one player on a team to all other players on all other teams.   </p>
  
  <p>With this example, the output should look like this:</p>
  
  <pre><code>m &lt;- matrix(c(4,2,1,2,5,1,1,2,5,2,0,2,5,2,1,1), nrow=4, ncol=4)
  colnames(m) &lt;- df$teams
  rownames(m) &lt;- df$teams
  
  &gt; m
    A B C D
  A 4 5 5 5
  B 2 1 2 2
  C 1 1 0 1
  D 2 2 2 1
  </code></pre>
  
  <p>Note that a player from team A can play with:</p>
  
  <ul>
  <li>4 other players from team A</li>
  <li>2 players from team B</li>
  <li>1 player from team C</li>
  <li>2 players from team D</li>
  </ul>
  
  <p>Does anyone know how to do this?</p>
  
  <p>The real data holds thousands of teams and millions of players. </p>