title: Comparing columns from 2 different files using R
body: <p>I have 2 files as follows</p>
  
  <p>File 1:</p>
  
  <pre><code>Locus   S1  S2  S3
  loc1    87  56  77
  loc2    34  55  75
  loc3    12  09  78
  loc4    34  67  89
  loc5    78  65  46
  </code></pre>
  
  <p>File 2:</p>
  
  <pre><code>Locus   S1  S2  S3
  loc3    13  43  34
  loc5    43  56  90
  loc7    89  56  33
  loc1    56  88  00
  loc4    66  77  98
  loc2    34  44  66
  </code></pre>
  
  <p>I want to compare/match the "Locus" columns from both the files, such that, in the "new_output file", i should have the sequence of the "locus" column from the File1 and values from the respective locus from File2.</p>
  
  <p>So my "new_output file" should look like this,</p>
  
  <pre><code>Locus   S1  S2  S3
  loc1    56  88  00
  loc2    34  44  66
  loc3    13  43  34
  loc4    66  77  98
  loc5    43  56  90
  </code></pre>
  
  <p>I tried something like,</p>
  
  <pre><code>file1 &lt;-read.delim(file="file1.txt",header=TRUE,sep="\t")
  file2 &lt;-read.delim(file="file2.txt",header=TRUE,sep="\t")
  new_output &lt;- file1[file1$Locus %in% file2$Locus,]
  write.table(new_output,file="new_output.txt",sep="\t")
  </code></pre>
  
  <p>But thats not really giving me the results the way i want. Can anyone help me with this? And tell me, where am i going wrong?</p>