我是R的新手,我需要建议处理这个问题:
我有2张桌子。表格的开头如下所示:
表1:
SNP Gene Pval Best_SNP Best_Pval
rs2932538 ENSG00000007341 5.6007
rs10488631 ENSG00000064419 7.7461
rs12537284 ENSG00000064419 4.5544
rs3764650 ENSG00000064666 12.3401
rs10479002 ENSG00000072682 5.0141
rs6704644 ENSG00000072682 6.2306
rs2900211 ENSG00000072682 9.9022
表2:
Best_SNP Gene Best_Pval
rs9028922 ENSG00000007341 10.7892
rs8233293 ENSG00000064666 89.342
rs3234432 ENSG00000072682 32.321
rs2892334 ENSG00000064419 43.235
表1包含每个基因的完整SNP列表。表2包含表1中出现的每种基因的最佳SNP和相应的最佳Pval。
我想要执行以下操作:匹配表1到表2中的每个Gene,然后从表2中复制Best_SNP和Best_Pval,并将它们粘贴到表1中针对该Gene的Best_SNP和Best_Pval列下。 棘手部分是在表1中,每个基因重复一个随机的,不同数量的行。例如,第二个基因ENSG00000064419重复2行,ENSG00000072682重复3行。因此,代码需要过滤基因的名称,并且只复制同一基因的Best_SNP和Best_Pval 一次。
因此,对于基因ENSG00000072682,在3行中,只有看起来包含该基因的第一行需要填充Best_SNP和Best_Pval列。我不希望2个重复行的其余部分也有填写了Best_SNP和Best_Pval列。可以更容易地看到每个基因的起点和终点。
答案 0 :(得分:0)
如果我理解正确,这就是解决方案:
x <- structure(list(SNP = c("rs2932538", "rs10488631", "rs12537284", "rs3764650",
"rs10479002", "rs6704644", "rs2900211"), Gene = c("ENSG00000007341", "ENSG00000064419",
"ENSG00000064419", "ENSG00000064666", "ENSG00000072682", "ENSG00000072682","ENSG00000072682"),
Pval= c(5.6007, 7.7461, 4.5544, 12.3401, 5.0141, 6.2306, 9.9022)), row.names= c(NA, 7L), class = "data.frame")
x
SNP Gene Pval
1 rs2932538 ENSG00000007341 5.6007
2 rs10488631 ENSG00000064419 7.7461
3 rs12537284 ENSG00000064419 4.5544
4 rs3764650 ENSG00000064666 12.3401
5 rs10479002 ENSG00000072682 5.0141
6 rs6704644 ENSG00000072682 6.2306
7 rs2900211 ENSG00000072682 9.9022
x1 <- x[!(duplicated(x$Gene) | duplicated(x$Gene, fromLast = FALSE)), ]
x1
SNP Gene Pval
1 rs2932538 ENSG00000007341 5.6007
2 rs10488631 ENSG00000064419 7.7461
4 rs3764650 ENSG00000064666 12.3401
5 rs10479002 ENSG00000072682 5.0141
y <- structure(list(Best_SNP = c("rs9028922", "rs8233293", "rs3234432", "rs2892334"), Gene = c("ENSG00000007341", "ENSG00000064666",
"ENSG00000072682", "ENSG00000064419" ),
Best_Pval= c(10.7892, 89.342, 32.321, 43.235)), row.names= c(NA, 4L), class = "data.frame")
y
Best_SNP Gene Best_Pval
1 rs9028922 ENSG00000007341 10.7892
2 rs8233293 ENSG00000064666 89.3420
3 rs3234432 ENSG00000072682 32.3210
4 rs2892334 ENSG00000064419 43.2350
merge(x1, y, by="Gene", all= FALSE)
Gene SNP Pval Best_SNP Best_Pval
1 ENSG00000007341 rs2932538 5.6007 rs9028922 10.7892
2 ENSG00000064419 rs10488631 7.7461 rs2892334 43.2350
3 ENSG00000064666 rs3764650 12.3401 rs8233293 89.3420
4 ENSG00000072682 rs10479002 5.0141 rs3234432 32.3210