title: A more general function for the number of matching elements among the n elements of a list
body: <p>I am trying to calculate the number of common entries among the elements of a list:</p>
  
  <pre><code>temp&lt;-list(element1=c("a","b","c"), element2=c("b","c","d"), 
             element3=c("d","e","f"), element4=c("a","c","z"))
  </code></pre>
  
  <p>To get the overlap among all pairwise combinations of the elements, this function works:</p>
  
  <pre><code>calculate.overlap.2&lt;-function(y){
    pw&lt;-combn(y,2,FUN=function(x)sum(x[[1]]%in%x[[2]]))
    names(pw)&lt;-combn(y,2,FUN=function(x)paste(names(x)[[1]],names(x)[[2]],sep="-"))
    return(pw)
  }
  </code></pre>
  
  <p>To get the overlap among all three-way combinations of the elements, this function works:</p>
  
  <pre><code>calculate.overlap.3&lt;-function(y){
     pw&lt;-combn(y,3,FUN=function(x)sum(x[[1]]%in%x[[2]]&amp;x[[1]]%in%x[[3]]))
     names(pw)&lt;-combn(y,3,FUN=function(x) paste(names(x)[[1]],names(x)[[2]],names(x)[[3]],sep="-"))
     return(pw)
  }
  </code></pre>
  
  <p>but as you can tell from the numbers inside the function, this is not an elegant solution.  </p>
  
  <p>It would be really nice to generalize these two functions into one, and have a function takes as an input the elements in each check of overlap.  That is, an input of  <code>number.of.elements.per.comparison=2</code> would be equivalent to <code>calculate.overlap.2</code> above and an input to the function of <code>number.of.elements.per.comparison=3</code> would be the same as <code>calculate.overlap.3</code>.  </p>
  
  <p>I feel like there is a very elegant solution to this, but I just can't see it.</p>