9

I am running a simulation study and need to process and save the results from several text files. I have the data organized in such a way where there are sub directories and within each sub directory, I need to process and get individual results for 1000 data files. This is very easy to do in SAS using macros. However, I am new to R and cannot figure out how to do such. Below is what I am trying to accomplish.

DATA Folder-> DC1 -> DC1R1.txt ... DC1R1000.txt
              DC2 -> DC2R1.txt ... DC2R1000.txt

Any help would be greatly appreciated!

pete
  • 2,327
  • 2
  • 15
  • 23
Stefanie
  • 91
  • 1
  • 1
  • 2

4 Answers4

12

I'm not near a computer with R right now, but read the help for file-related functions:

The dir function will list the files and directories. It has a recursive argument. list.files is an alias for dir. The file.info function will tell you (among other things) if a path is a directory and file.path will combine path parts.

The basename and dirname functions might also be useful.

Note that all these functions are vectorized.

EDIT Now at a computer, so here's an example:

# Make a function to process each file
processFile <- function(f) {
  df <- read.csv(f)
  # ...and do stuff...
  file.info(f)$size # dummy result
}

# Find all .csv files
files <- dir("/foo/bar/", recursive=TRUE, full.names=TRUE, pattern="\\.csv$")

# Apply the function to all files.
result <- sapply(files, processFile)
Tommy
  • 39,997
  • 12
  • 90
  • 85
  • Thank you all for your responses! @Tommy.....Your coding is working so far....fingers crossed....I have 768 data conditions with 1000 simulated files under each condition. It will take awhile to process, so I will give a status once finished. – Stefanie Sep 11 '11 at 20:59
8

If you need to run the same analysis on each of the files, then you can access them in one shot using list.files(recursive = T). This is assuming that you have already set your working directory to Data Folder. The recursive = T lists all files within subdirectories as well.

Ramnath
  • 54,439
  • 16
  • 125
  • 152
5

filenames <- list.files("path/to/files", recursive=TRUE) This will give you all the files residing under one folder and sub folders under it.

Aditi
  • 820
  • 11
  • 27
0

You can use Perl's glob () function to get a list of files and send it to R using, e.g., RSPerl's interface.

Itamar
  • 2,111
  • 13
  • 16
  • Hi Ltamar. I am not familiar with Perl. I am a statistician and have never had the need to use such. Although....I hear it is quite powerful. – Stefanie Sep 11 '11 at 21:06