根据模式列表的子集文件

时间:2017-02-17 15:58:53

标签: r

我有一个名为例如的文件列表:

"Experiment_myex1_test1"     
"Experiment_myex1_test2"      
"Experiment_myex1_test3"       
"Experiment_myex2_test1"      
"Experiment_myex2_test2"    
"Experiment_myex2_test3"      
"Experiment_myex3_test1"      
"Experiment_myex3_test2"    
"Experiment_myex3_test3"    

等等(总共1000个文件)和一个如下所示的文件.txt:

df
List_of_ex      
myex1     
myex1    
myex1 
myex2     
myex2      
myex2
myex4    
myex4     
myex4     

(由于* test1,* test2和* test3,它们一式三份出现。

我想从list.files()获取与df[,1]中的模式匹配的文件列表。
换句话说,我想得到以下列表:

"Experiment_myex1_test1"     
"Experiment_myex1_test2"      
"Experiment_myex1_test3"       
"Experiment_myex2_test1"      
"Experiment_myex2_test2"    
"Experiment_myex2_test3"      

有人能帮帮我吗?

提前谢谢

最佳

1 个答案:

答案 0 :(得分:1)

假设您确定您将始终拥有" test1"," test2"和" test3",您可以使用正则表达式grep执行此任务。

lapply(df[, 1], function(pattern_ex) {
  lapply(c("test1", "test2", "test3"), function(pattern_test){
    grep(pattern = paste0(pattern_ex, ".*", pattern_test, "$"), 
         x = list.files(),
         value = T)
  })
})