繁体   English   中英

list.files()无法获取正确的列表

[英]list.files() cannot get the correct list

我有一个文件功能列表,看起来像这样:

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开头的目录中的文件

我尝试了许多不同的方法,但对我没有任何帮助: 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" 

list.files将与该模式不匹配完整的文件名。 与上面的注释类似,另一种方法是将grep应用于结果,或将其应用于您作为参数给出的路径列表,例如:

files <- rev( list.files( grep( "Sample_73", list.dirs( "../runs" ), value=TRUE), "*.gene.read.count", full.names=TRUE))

正如celiomsj提到的, pattern的参数list.files只匹配的文件名,而不是路径,所以这是最好的两条线进行。 以下答案使用str_detect作为str_detect的更易读的替代grepl

library(stringr)
files <- list.files(
  "../runs",
  pattern    = glob2rx("*.gene.read.count"),
  full.names = TRUE, 
  recursive  = TRUE
)
files <- files[str_detect(files, fixed("Sample_73"))]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM