Kumar Mainali
2012-Apr-01 22:51 UTC
[R] Selecting files with a particular string in filename
I am trying to find an efficient way to select certain text files from a folder with thousands of text files. I used file.list function with a specific "pattern" to obtain 70 files for each of the i's in 1:100. Next, I need to select some files from this list of 70 files. The files to be selected do have a unique "pattern" in their file name. However, I cannot use list.files function because it looks for a specific path. Any help is appreciated. Thanks in advance. - Kumar for (i in 1:100) { name_rep = paste("rep_",i,"_",sep="") files <- list.files(path="C:\\Users\\Binn\\Desktop\\script_output", pattern= name_rep, full.names=TRUE ) natfiles <- list.files(path=files, pattern= "_natrain.csv", full.names=TRUE ) <OPERATIONS HERE> } -- Section of Integrative Biology University of Texas at Austin Austin, Texas 78712, USA [[alternative HTML version deleted]]
Rolf Turner
2012-Apr-02 10:09 UTC
[R] Selecting files with a particular string in filename
Doesn't grep() solve your problem? cheers, Rolf Turner On 02/04/12 10:51, Kumar Mainali wrote:> I am trying to find an efficient way to select certain text files from a > folder with thousands of text files. I used file.list function with a > specific "pattern" to obtain 70 files for each of the i's in 1:100. Next, I > need to select some files from this list of 70 files. The files to be > selected do have a unique "pattern" in their file name. However, I cannot > use list.files function because it looks for a specific path. Any help is > appreciated. > > Thanks in advance. > > - Kumar > > for (i in 1:100) { > name_rep = paste("rep_",i,"_",sep="") > files<- list.files(path="C:\\Users\\Binn\\Desktop\\script_output", > pattern= name_rep, full.names=TRUE ) > natfiles<- list.files(path=files, pattern= "_natrain.csv", full.names=TRUE > ) > <OPERATIONS HERE> > } > >