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 wit
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)