2015-04-03 22 views
0

我有两个文件,我想执行一个grep,其中file1是模式。我想从file2中提取匹配file1中字符串的行。 file1中的字符串位于file2中的“Target.Gene”列中。我不知道如何使用此类型的图案使用模式的两个文件之间的grep

文件1

> head(a) 
    symbol 
1 AGER 
2 TCF21 
3 CLDN5 
4 CDH5 
5 CA4 
6 RAMP3 

文件2

> head(MTI) 
     miRTarBase.ID   miRNA Species..miRNA. Target.Gene Target.Gene..Entrez.ID. Species..Target.Gene. 
    1 MIRT006481 hsa-miR-181a-5p Homo sapiens  DUSP6     1848   Homo sapiens 
    2 MIRT000002 hsa-miR-20a-5p Homo sapiens  HIF1A     3091   Homo sapiens 
    3 MIRT000006 hsa-miR-146a-5p Homo sapiens  CXCR4     7852   Homo sapiens 
    4 MIRT000006 hsa-miR-146a-5p Homo sapiens  CXCR4     7852   Homo sapiens 
    5 MIRT006511 hsa-miR-200b-3p Homo sapiens  RND3      390   Homo sapiens 
    6 MIRT006477 hsa-miR-328-3p Homo sapiens  PTPRJ     5795   Homo sapiens 
                  Experiments   Support.Type References..PMID. 
    1       Luciferase reporter assay//Western blot  Functional MTI   17382377 
    2 Luciferase reporter assay//Western blot//Northern blot//qRT-PCR  Functional MTI   18632605 
    3    qRT-PCR//Luciferase reporter assay//Western blot  Functional MTI   18568019 
    4              Microarray Functional MTI (Weak)   20375304 
    5          Luciferase reporter assay  Functional MTI   20683643 
    6           qRT-PCR//Western blot  Functional MTI   22564856 

回答

1

我觉得merge是什么您这里需要。这是否给你你想要的结果:

mergedDat = merge(file1, file2, by.x="symbol", by.y="Target.Gene", all.x=TRUE) 

这将返回的file1所有行无论在file1给定值是否在file2匹配。如果你只想要匹配的行,只需删除all.x=TRUE。关于merge的帮助,请参阅有关功能在不匹配时执行的操作的所有选项。

2

您也可以通过索引和使用%in%运算符找到它的子集。下面的代码将过滤MTI,以便结果数据帧只有MTI行,其中Target.Gene是所有列中的基因之一。

result <- MTI[MTI$Target.Gene %>% a, ] 
相关问题