Skip to content
Back to formatted view

Raw Message

Message-ID: <494FDAE3.2060600@stats.uwo.ca>
Date: 2008-12-22T18:22:27Z
From: Duncan Murdoch
Subject: Globbing Files in R
In-Reply-To: <giolev$sra$1@ger.gmane.org>

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.