[R] Globbing Files in R

Duncan Murdoch murdoch at stats.uwo.ca
Mon Dec 22 19:22:27 CET 2008


On 12/22/2008 1:14 PM, Earl F Glynn wrote:
> 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))

Remember to use seq_along() instead:  ProcessList might be length 0.

Duncan Murdoch

> {
>    filename <- ProcessList[i]
>    . . .
> }
> 
> 
> efg
> 
> Earl F Glynn
> Overland Park, KS
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list