Skip to content

iterating over files in a directory with R

6 messages · femke, Tom Blackwell, Hadley Wickham +3 more

#
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:

            
#
?list.files

?"for"

Hadley
femke wrote:

            
#
On Sun, 8 Feb 2004 16:07:44 -0500, you wrote:

            
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:

            
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: