Hello, I'm an R newbie and was wondering whether there are R commands for iterating over files in a directory. Basically what I want to do is to iterate over many files and apply some R functions to each file seperately. e.g. for (each file in a directory) do { some R calculation with the info in that file } Could someone please give me a pointer to how (or if) this might be done with R - I haven't yet been able to find anything relevant in the manuals. Or would I need to call each file with some language such as Java and then use R once I have the file name? Thanks for your help, femke [[alternative HTML version deleted]]
Try help("list.files"), help("file.info"), help("file"), and look in the "see also" section in each of those help files for other functions which you may find useful for doing this. - tom blackwell - u michigan medical school - ann arbor - On Sun, 8 Feb 2004, femke wrote:> Hello, > > I'm an R newbie and was wondering whether there are R commands for iterating over files in a directory. Basically what I want to do is to iterate over many files and apply some R functions to each file seperately. > > e.g. for (each file in a directory) do { some R calculation with the info in that file } > > Could someone please give me a pointer to how (or if) this might be done with R - I haven't yet been able to find anything relevant in the manuals. Or would I need to call each file with some language such as Java and then use R once I have the file name? > > Thanks for your help, > > femke > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
?list.files ?"for" Hadley femke wrote:> Hello, > > I'm an R newbie and was wondering whether there are R commands for iterating over files in a directory. Basically what I want to do is to iterate over many files and apply some R functions to each file seperately. > > e.g. for (each file in a directory) do { some R calculation with the info in that file } > > Could someone please give me a pointer to how (or if) this might be done with R - I haven't yet been able to find anything relevant in the manuals. Or would I need to call each file with some language such as Java and then use R once I have the file name? > > Thanks for your help, > > femke > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
On Sun, 8 Feb 2004 16:07:44 -0500, you wrote:>Hello, > >I'm an R newbie and was wondering whether there are R commands for iterating over files in a directory. Basically what I want to do is to iterate over many files and apply some R functions to each file seperately.The apply() and related functions do iteration over various things, and list.files() and choose.files() select files (the latter interactively on Windows). So something like this might do what you want: lapply(list.files(), function(x) paste('filename is ',x)) Duncan Murdoch
On Sun, 8 Feb 2004, femke wrote:> Hello, > > I'm an R newbie and was wondering whether there are R commands for > iterating over files in a directory. Basically what I want to do is to > iterate over many files and apply some R functions to each file > seperately. > > e.g. for (each file in a directory) do { some R calculation with the > info in that file } >sapply(list.files("a directory"), some.R.calculation) or files<-list.files("a directory") for(each.file in files){ some.R.calculation } -thomas
I usually do something like this: filelist <- dir(path = ".") ## files in current directory for(filename in filelist) { do.something(filename) } -roger femke wrote:> Hello, > > I'm an R newbie and was wondering whether there are R commands for iterating over files in a directory. Basically what I want to do is to iterate over many files and apply some R functions to each file seperately. > > e.g. for (each file in a directory) do { some R calculation with the info in that file } > > Could someone please give me a pointer to how (or if) this might be done with R - I haven't yet been able to find anything relevant in the manuals. Or would I need to call each file with some language such as Java and then use R once I have the file name? > > Thanks for your help, > > femke > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >