我有一个文件列表函数,如下所示:
files <- rev( list.files( list.dirs( "../runs" ), "*.gene.read.count", full.names=TRUE ) )
这会产生以下结果:
> files
[1] "../runs/Sample_7316/7316_AACCGA_L003_R.gene.read.count"
[2] "../runs/Sample_7315/7315_GAATCT_L003_R.gene.read.count"
[3] "../runs/Sample_7314/7314_CCTTGC_L003_R.gene.read.count"
[4] "../runs/Sample_7313/7313_AGGCCA_L003_R.gene.read.count"
[5] "../runs/Sample_7312/7312_GCGAAG_L003_R.gene.read.count"
[6] "../runs/Sample_7311/7311_TCTCAG_L003_R.gene.read.count"
[7] "../runs/Sample_7310/7310_CTCTGG_L003_R.gene.read.count"
[8] "../runs/Sample_7309/7309_ATGGCG_L008_R.gene.read.count"
[9] "../runs/project-043.Allreports/project043.raw.gene.read.count.hetro.homo.csv"
[10] "../runs/project-043.Allreports/project043.raw.gene.read.count.csv"
[11] "../runs/project-043.Allreports/project043.gene.read.count.hetro.homo.csv"
[12] "../runs/project-043.Allreports/project043.gene.read.count.csv"
[13] "../runs/project-043.Allreports/analysis-project043.raw.gene.read.count.html"
[14] "../runs/project-043.Allreports/analysis-project043.raw.gene.read.count.hetro.homo.html"
[15] "../runs/project-043.Allreports/analysis- project043.gene.read.count.html"
[16] "../runs/project-043.Allreports/analysis-project043.gene.read.count.hetro.homo.html"
问题是我只需要从Sample_73
开始的dirs中的文件我尝试过很多不同的东西,但对我来说没有任何作用:
files <- rev( list.files( list.dirs( "../runs/Sample*" ), "*.gene.read.count", full.names=TRUE ) )
希望有办法只选择这些目录:
[1] "../runs/Sample_7316/7316_AACCGA_L003_R.gene.read.count"
[2] "../runs/Sample_7315/7315_GAATCT_L003_R.gene.read.count"
[3] "../runs/Sample_7314/7314_CCTTGC_L003_R.gene.read.count"
[4] "../runs/Sample_7313/7313_AGGCCA_L003_R.gene.read.count"
[5] "../runs/Sample_7312/7312_GCGAAG_L003_R.gene.read.count"
[6] "../runs/Sample_7311/7311_TCTCAG_L003_R.gene.read.count"
[7] "../runs/Sample_7310/7310_CTCTGG_L003_R.gene.read.count"
[8] "../runs/Sample_7309/7309_ATGGCG_L008_R.gene.read.count"
答案 0 :(得分:0)
list.files
与模式与完整文件名不匹配。与上面的注释类似,另一种方法是将grep
应用于结果,或将其应用于您作为参数提供的路径列表,例如:
files <- rev( list.files( grep( "Sample_73", list.dirs( "../runs" ), value=TRUE), "*.gene.read.count", full.names=TRUE))
答案 1 :(得分:0)
如celiomsj所述,pattern
的{{1}}参数仅匹配文件名,而不是路径,所以最好用两行完成。以下答案使用list.files
作为str_detect
的可读替代方案。
grepl