Dear all,
For example I want to process set of files.
Typically Perl's idiom would be:
__BEGIN__
@files = glob("/mydir/*.txt");
foreach my $file (@files) {
# process the file
}
__END__
What's the R's way to do that?
- Gundala Viswanath
Jakarta - Indonesia
Globbing Files in R
6 messages · Gundala Viswanath, Douglas Bates, Gabor Grothendieck +3 more
On Sun, Dec 21, 2008 at 9:35 AM, Gundala Viswanath <gundalav at gmail.com> wrote:
Dear all,
For example I want to process set of files.
Typically Perl's idiom would be:
__BEGIN__
@files = glob("/mydir/*.txt");
foreach my $file (@files) {
# process the file
}
__END__
What's the R's way to do that?
The tools to do this are the functions list.files, grep (or variants on grep) and perhaps glob2rx. See the help files for each. One approach is
files <- list.files("~/Desktop")
txtfiles <- files[grep(glob2rx("*.txt"), files)]
txtfiles
[1] "notes312.txt" "stat324.txt" Note that grep returns a vector of indices into the character vector, not the character vectors themselves.
Try this:
file.names <- dir(pattern = glob2rx("/mydir/*.txt"))
for(fn in file.names) {
DF <- read.table(fn, ...)
...
}
Another possibility is:
file.names <- .. as above ...
out <- lapply(file.names, function(fn) {
DF <- read.table(fn, ...)
...
})
out will have one component per file formed from the result of the
each function application.
On Sun, Dec 21, 2008 at 10:35 AM, Gundala Viswanath <gundalav at gmail.com> wrote:
Dear all,
For example I want to process set of files.
Typically Perl's idiom would be:
__BEGIN__
@files = glob("/mydir/*.txt");
foreach my $file (@files) {
# process the file
}
__END__
What's the R's way to do that?
- Gundala Viswanath
Jakarta - Indonesia
______________________________________________ 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.
Sys.glob is much more direct .... Education of you might find exploring the power of?? (e.g. ??glob) educational.
On Sun, 21 Dec 2008, Douglas Bates wrote:
On Sun, Dec 21, 2008 at 9:35 AM, Gundala Viswanath <gundalav at gmail.com> wrote:
Dear all,
For example I want to process set of files.
Typically Perl's idiom would be:
__BEGIN__
@files = glob("/mydir/*.txt");
foreach my $file (@files) {
# process the file
}
__END__
What's the R's way to do that?
One of them is exactly the same idiom.
The tools to do this are the functions list.files, grep (or variants on grep) and perhaps glob2rx. See the help files for each. One approach is
files <- list.files("~/Desktop")
txtfiles <- files[grep(glob2rx("*.txt"), files)]
txtfiles
[1] "notes312.txt" "stat324.txt" Note that grep returns a vector of indices into the character vector, not the character vectors themselves.
______________________________________________ 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.
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
1 day later
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]
. . .
}
efg
Earl F Glynn
Overland Park, KS
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.