R语言中批量提取当前目录中指定类型的文件

1、直接测试

dir()     ## 列出当前目录中所有文件
myfile <- list.files()   ## 将当前目录中的所有文件都保存为字符
myfile
gzfile <- myfile[grep(myfile, pattern =".gz$")]   ## 提取当前目录中以.gz结尾的文件
gzfile
txtfile <- myfile[grep(myfile, pattern = ".txt$")]   ## 提取当前目录中以.txt结尾的文件
txtfile

 

> dir()   ## 列出所有文件
[1] "GSE148812_family.soft"                   
[2] "GSE148812_family.soft.gz"                
[3] "GSE148812_genotyping_results.txt"        
[4] "GSE148812_genotyping_results.txt.gz"     
[5] "GSE148812_RAW.tar"                       
[6] "GSM4481589_8378315049_R05C01_Grn.idat"   
[7] "GSM4481589_8378315049_R05C01_Grn.idat.gz"
[8] "GSM4481589_8378315049_R05C01_Red.idat"   
[9] "GSM4481589_8378315049_R05C01_Red.idat.gz"
> myfile <- list.files()   ## 所有文件赋值给一个变量
> myfile
[1] "GSE148812_family.soft"                   
[2] "GSE148812_family.soft.gz"                
[3] "GSE148812_genotyping_results.txt"        
[4] "GSE148812_genotyping_results.txt.gz"     
[5] "GSE148812_RAW.tar"                       
[6] "GSM4481589_8378315049_R05C01_Grn.idat"   
[7] "GSM4481589_8378315049_R05C01_Grn.idat.gz"
[8] "GSM4481589_8378315049_R05C01_Red.idat"   
[9] "GSM4481589_8378315049_R05C01_Red.idat.gz"
> gzfile <- myfile[grep(myfile, pattern =".gz$")]   ## 提取以.gz结尾的文件
> gzfile
[1] "GSE148812_family.soft.gz"                
[2] "GSE148812_genotyping_results.txt.gz"     
[3] "GSM4481589_8378315049_R05C01_Grn.idat.gz"
[4] "GSM4481589_8378315049_R05C01_Red.idat.gz"
> txtfile <- myfile[grep(myfile, pattern = ".txt$")]   ##提取以.txt结尾的文件
> txtfile
[1] "GSE148812_genotyping_results.txt"

 

> gsefile <- myfile[grep(myfile, pattern = "^GSE")]   ## 提取以GSE开头的文件
> gsefile
[1] "GSE148812_family.soft"              
[2] "GSE148812_family.soft.gz"           
[3] "GSE148812_genotyping_results.txt"   
[4] "GSE148812_genotyping_results.txt.gz"
[5] "GSE148812_RAW.tar"    

 

参考:https://zhuanlan.zhihu.com/p/378404523

 

posted @ 2021-12-18 22:40  小鲨鱼2018  阅读(1290)  评论(0编辑  收藏  举报