[R] Globbing Files in R

Earl F Glynn efglynn at gmail.com
Mon Dec 22 19:14:45 CET 2008

Gundala Viswanath wrote:

> Typically  Perl's idiom would be:
> __BEGIN__
> @files = glob("/mydir/*.txt");
> foreach my $file (@files) {
>   # process the file
> }
> __END__

Something like this has been suggested in R-help before:

files <- dir()
results <- lapply(files, yourprocessing())

The dir function has path and pattern arguments to select the set of 
files you want.

This works fine when there are no problems, but often I'll use a for 
loop so problem files can be dealt with differently when necessary.

Perhaps something like this:

ProcessList <- dir(pattern="InPerson*")

for (i in 1:length(ProcessList))
   filename <- ProcessList[i]
   . . .


Earl F Glynn
Overland Park, KS

More information about the R-help mailing list