title: R create matrix from one variable
body: <p>I would like to create a matrix from one variable in my data frame. Each cell of matrix will be filled like this:</p>
  
  <pre><code>sum(Rin1,Rin2)/sum(Rin),...,sum(Rin1,Rin28)/sum(Rin)
  sum(Rin2,Rin3)/sum(Rin),...,sum(Rin2,Rin28)/sum(Rin)
  </code></pre>
  
  <p>I use this script but it didn't work for me:</p>
  
  <pre><code># Keep one column that has proportion of Rin=Tdin/User.in 
  df.in &lt;- df[,c(5)]
  # Create matrix for rows' combination
  a&lt;-t(combn(28,2))
  # Create empty matrix according length of rows
  output &lt;- matrix ('',28,28)
  # Fill matrix with sum of rows divide sum of Rin 
  for (i in 1:nrow(a)) {
    # I came up with this line from my previous work! 
    output[cbind(a[i,][1],a[i,][2])] &lt;-unlist(intersect(df.in[a[i,]][1],df.in[a[i,]][2]))
  }
  </code></pre>
  
  <p>Thanks in advance for any suggestion.</p>
  
  <p>Sample data frame: </p>
  
  <pre><code>df &lt;- structure(list(Subject = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
                                             1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                             1L, 1L, 1L, 1L, 1L, 1L),
                  .Label = c("Political.Issue", "Women.s.Rights", "Workers..Rights"), 
                  class = "factor"), Kcore.in = c(0, 1, 2,3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 21, 
                  22, 25,28, 30, 39, 40, 41, 42, 44, 82), Tdin = c(2090, 3217, 6110, 32427,6612, 3425, 11940,
                  17985, 1998, 882, 8661, 3228, 701, 656, 269,393, 7737, 339, 6456, 1950, 99, 302, 43320, 1087,
                  3655, 2103,562, 2135), User.in = c(4723L, 183L, 87L, 62L, 24L, 13L, 13L,25L, 7L, 2L, 9L, 9L, 
                  2L, 2L, 1L, 1L, 13L, 2L, 3L, 3L, 1L, 1L,4L, 1L, 2L, 2L, 1L, 2L), Rin = c(0.442515350412873,
                  17.5792349726776,70.2298850574713, 523.016129032258, 275.5, 263.461538461538,918.461538461538,
                  719.4, 285.428571428571, 441, 962.333333333333,358.666666666667, 350.5, 328, 269, 393, 595.153846153846, 
                  169.5,2152, 650, 99, 302, 10830, 1087, 1827.5, 1051.5, 562, 1067.5)), .Names = c("Subject","Kcore.in", "Tdin", 
                  "User.in", "Rin"), row.names = c(NA, -28L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = "Subject", 
                  drop = TRUE, indices = list( 0:27), group_sizes = 28L, biggest_group_size = 28L, labels = structure(list(Subject = 
                  structure(1L, .Label = c("Political.Issue", "Women.s.Rights","Workers..Rights"), class = "factor")), 
                  row.names = c(NA,-1L), class = "data.frame", vars = "Subject", drop = TRUE, .Names = "Subject"))
  </code></pre>