title: Generalize R %in% operator to match tuples
body: <p>I spent a while the other day looking for a way to check if a row vector is contained in some set of row vectors in R.  Basically, I want to generalize the <code>%in%</code> operator to match a tuple instead of each entry in a vector.  For example, I want:</p>
  
  <pre><code>row.vec = c("A", 3)
  row.vec
  # [1] "A" "3"
  
  data.set = rbind(c("A",1),c("B",3),c("C",2))
  data.set
  #      [,1] [,2]
  # [1,] "A"  "1" 
  # [2,] "B"  "3" 
  # [3,] "C"  "2" 
  
  row.vec %tuple.in% data.set
  # [1] FALSE
  </code></pre>
  
  <p>for my made-up operator <code>%tuple.in%</code> because the row vector <code>c("A",3)</code> is not a row vector in data.set.  Using the <code>%in%</code> operator gives:</p>
  
  <pre><code>row.vec %in% data.set
  # [1] TRUE TRUE
  </code></pre>
  
  <p>because "A" and 3 are in <code>data.set</code>, which is not what I want.</p>
  
  <p>I have two questions.  First, are there any good existing solutions to this?</p>
  
  <p>Second, since I couldn't find them (even if they exist), I tried to write my own function to do it.  It works for an input matrix of row vectors, but I'm wondering if any experts have proposed improvements:</p>
  
  <pre><code>is.tuple.in &lt;- function(matrix1, matrix2){
  
       # Apply rbind() so that matrix1 has columns even if it is a row vector.
       matrix1 = rbind(matrix1)
  
       if(ncol(matrix1) != ncol(matrix2)){ 
        stop("Matrices must have the same number of columns.") }
  
       # Now check for the first row and handle other rows recursively
       row.vec = matrix1[1,]
       tuple.found = FALSE
       for(i in 1:nrow(matrix2)){
            # If we find a match, then this row exists in matrix 2 and we can break the loop
            if(all(row.vec == matrix2[i,])){
                 tuple.found = TRUE
                 break
            }
       }
  
       # If there are more rows to be checked, use a recursive call
       if(nrow(matrix1) &gt; 1){
            return(c(tuple.found, is.tuple.in(matrix1[2:nrow(matrix1),],matrix2)))
       } else {
            return(tuple.found)
       }
  }
  </code></pre>
  
  <p>I see a couple problems with that that I'm not sure how to fix.  First, I'd like the base case to be clear at the start of the function.  I didn't manage to do this because I pass <code>matrix1[2:nrow(matrix1),]</code> in the recursive call, which produces an error if <code>matrix1</code> has one row.  So instead of getting to a case where <code>matrix1</code> is empty, I have an if condition at the end deciding if more iterations are necessary.</p>
  
  <p>Second, I think the use of <code>rbind()</code> at the start is sloppy, but I needed it for when <code>matrix1</code> had been reduced to a single row.  Without using <code>rbind()</code>, <code>ncol(matrix1)</code> produced an error in the 1-row case.  I figure my trouble here has to do with a lack of knowledge about R data types.</p>
  
  <p>Any help would be appreciated.</p>