2012-10-02 35 views
0

我有2個文件如下從,使用R

文件1 2個不同的文件比較列:

Locus S1 S2 S3 
loc1 87 56 77 
loc2 34 55 75 
loc3 12 09 78 
loc4 34 67 89 
loc5 78 65 46 

文件2:

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 

我要比較/匹配「軌跡「這兩個文件中的列,例如,在」new_output文件「中,我應該有來自File1的」locus「列的序列以及來自File2的各個基因座的值。

所以我的「new_output文件」應該是這樣的,

Locus S1 S2 S3 
loc1 56 88 00 
loc2 34 44 66 
loc3 13 43 34 
loc4 66 77 98 
loc5 43 56 90 

我想是這樣,

file1 <-read.delim(file="file1.txt",header=TRUE,sep="\t") 
file2 <-read.delim(file="file2.txt",header=TRUE,sep="\t") 
new_output <- file1[file1$Locus %in% file2$Locus,] 
write.table(new_output,file="new_output.txt",sep="\t") 

但那不是真的給我,結果我想要的方式。誰能幫我這個?並告訴我,我哪裏錯了?

回答

1

您可以使用功能merge

merge(file1["Locus"], file2, by = "Locus") 

# Locus S1 S2 S3 
#1 loc1 56 88 0 
#2 loc2 34 44 66 
#3 loc3 13 43 34 
#4 loc4 66 77 98 
#5 loc5 43 56 90 
+0

謝謝您的幫助:) – Letin

1

你接近。取而代之的%in%,使用match

new_output <- file2[match(file1$Locus, file2$Locus), ] 

你也可以做的是用什麼作爲Locus行名:

file1 <-read.delim(file = "file1.txt", header = TRUE, sep = "\t", row.names = 1) 
file2 <-read.delim(file = "file2.txt", header = TRUE, sep = "\t", row.names = 1) 
new_output <- file2[rownames(file1$Locus), ] 
+0

謝謝您的幫助:) – Letin