title: pairwise comparison of multiple columns with different lengths
body: <p>I have a table in R with 13 columns and 6k odd rows in total, each column has different lengths. I want to produce a new table where I have compared col1 to col2 and so on and same for col2 to ncol until all columns covered. </p>
  
  <p>The new table's first column should look like something like this if there is a match if not <code>NA</code> should be in place of the chromosome location:</p>
  
  <pre><code>CTC1vCTC2
  chr6:86324663:T&gt;C
  </code></pre>
  
  <p>as an example this is a subsample of the data set i am trying to work on, this is only the first four columns, ideally I want to compare each chromosomal location(and the mutation) and make sure they match exactly:</p>
  
  <pre><code>    CTC1    CTC2    CTC3    CTC4
      chr1:2333588:C&gt;T    chr1:902108:C&gt;T chr1:3544992:T&gt;A    chr1:5924518:G&gt;A
      chr1:2938989:G&gt;A    chr1:1262966:C&gt;T    chr1:6021929:G&gt;T          chr1:5965381:C&gt;T
      chr1:3389727:C&gt;T    chr1:1325657:G&gt;A    chr1:6273227:G&gt;A    chr1:6279370:G&gt;C
  </code></pre>
  
  <p>I tried this so far, inspired by the answer below:</p>
  
  <pre><code>match_table &lt;- function(table){
    #the output table will have 78 columns
    new_table =data.frame(matrix("NA", nrow = nrow(table), ncol = 78))  
    for(i in ncol(table)){
      for(j in (i+1):ncol(table)){
        for (k in nrow(table)){
          if(table[k,i] == table[k,j] &amp;&amp; !is.null(table[k,i])){
            new_table[k,i] &lt;- table[k,i]
          } 
          else if(is.na(table[k,i])){
            new_table[k,i]='N'
          }
        }
        }
    }
    return(new_table)
  }
  </code></pre>
  
  <p>This returns the following error:</p>
  
  <p><code>Error in if (table[k, i] == table[k, j] &amp;&amp; !is.null(table[k, i])) { : 
    missing value where TRUE/FALSE needed</code></p>
  
  <p>I am probably missing something obvious, any help is greatly appreciated.</p>
  
  <p>Thanks</p>