title: faster way to compare rows in a data frame
body: <p>Consider the data frame below. I want to compare each row with rows below and then take the rows that are equal in more than 3 values.</p>
  
  <p>I wrote the code below, but it is very slow if you have a large data frame.</p>
  
  <p>How could I do that faster?</p>
  
  <pre><code>data &lt;- as.data.frame(matrix(c(10,11,10,13,9,10,11,10,14,9,10,10,8,12,9,10,11,10,13,9,13,13,10,13,9), nrow=5, byrow=T))
  rownames(data)&lt;-c("sample_1","sample_2","sample_3","sample_4","sample_5")
  
  &gt;data
            V1 V2 V3 V4 V5
  sample_1  10 11 10 13  9
  sample_2  10 11 10 14  9
  sample_3  10 10  8 12  9
  sample_4  10 11 10 13  9
  sample_5  13 13 10 13  9
  
  output &lt;- data.frame(sample = NA, duplicate = NA, matches = NA)
  dfrow &lt;- 1
  for(i in 1:nrow(data)) {
      sample &lt;- data[i, ]
      for(j in (i+1):nrow(data)) if(i+1 &lt;= nrow(data)) {
      matches &lt;- 0
          for(V in 1:ncol(data)) {
              if(data[j,V] == sample[,V]) {       
                  matches &lt;- matches + 1
              }
          }
          if(matches &gt; 3) {
              duplicate &lt;- data[j, ]
              pair &lt;- cbind(rownames(sample), rownames(duplicate), matches)
              output[dfrow, ] &lt;- pair
              dfrow &lt;- dfrow + 1
          }
      }
  }
  
  &gt;output
     sample    duplicate    matches
  1 sample_1   sample_2     4
  2 sample_1   sample_4     5
  3 sample_2   sample_4     4
  </code></pre>