title: Function on all column combinations of n*m matrix yielding a m*m result
body: <p>Say I have a function which takes two vectors and returns a single integer, for instance the number of elements in one that is also in the other vector. Like:</p>
  
  <pre><code>f &lt;- function(v1,v2)sum(v1 %in% v2)
  </code></pre>
  
  <p>How can I apply that function to all pairwise combinations of m columns in a n*m matrix. </p>
  
  <pre><code>set.seed(1)
  m &lt;- replicate(3, sample(letters[1:10], size = 5))
  dimnames(m) &lt;- list(NULL, paste0('c', 1:ncol(m)))
  </code></pre>
  
  <p>Now,</p>
  
  <pre><code>&gt; m
       [,1] [,2] [,3]
  [1,] "c"  "i"  "c" 
  [2,] "d"  "j"  "b" 
  [3,] "e"  "f"  "f" 
  [4,] "g"  "e"  "j" 
  [5,] "b"  "a"  "e" 
  </code></pre>
  
  <p>And take the function on the first two columns:</p>
  
  <pre><code>&gt; f(m[,1], m[,2])
  [1] 1 #'e' is shared.
  </code></pre>
  
  <p>How to do that on all column combinations ? The result could be a m*m matrix (where the results are symmetric around the diagonal) or, alternative, it could be a long-format data frame with columns for v1, v2 and the function's result (e.g. the first row would be <code>c1</code>, <code>c2</code> and <code>3</code> ) </p>
  
  <p>I tried to investigate the functions <code>outer</code> and <code>expand.grid</code> but could not find a solution.</p>