title: Speed Doing Many Joins in R
body: <p>I have a data frame similar this one:</p>
  
  <pre><code>n = c(rep("x", 3), rep("y", 5), rep("z", 2)) 
  s = c("aa", "bb", "cc", "dd", "ee", "aa", "bb", "cc", "dd", "ff") 
  df = data.frame(n, s) 
  </code></pre>
  
  <p>I want to find the number of matches for each unique df$n with every other df$n if I were to join them on df$s.  The following works, but it is very slow, and I have  large dataset.  Is there a faster way to approach this problem?</p>
  
  <pre><code>place &lt;- unique(df$n)
  df_answer &lt;- data.frame(place1 ="test1", place2 = "test2", matches = 2)
  for(i in place) { 
    for(k in place) { 
      m1 &lt;- inner_join(filter(df, n == i), filter(df, n == k), by = "s")
      m2 &lt;- data.frame(place1 = i, place2 = k, matches = length(m1$s))
      df_answer &lt;- rbind(df_answer, m2)
    } 
  } 
  df_answer &lt;- filter(df_answer, place1 != "test1")
  </code></pre>