Skip to content

Instantly share code, notes, and snippets.

@apreshill
Created March 18, 2016 14:50
Show Gist options
  • Save apreshill/1d197a3403aa4483454f to your computer and use it in GitHub Desktop.
Save apreshill/1d197a3403aa4483454f to your computer and use it in GitHub Desktop.
Read multiple csv files into R
# stack overflow answer from Joran Ellis:
# http://stackoverflow.com/questions/5319839/read-multiple-csv-files-into-separate-data-frames
# If the path is different than your working directory
# you'll need to set full.names = TRUE to get the full
# paths.
my_files <- list.files("path/to/files")
# Further arguments to read.csv can be passed in ...
all_csv <- lapply(my_files,read.csv,...)
# Set the name of each list element to its
# respective file name. Note full.names = FALSE to
# get only the file names, not the full path.
names(all_csv) <- gsub(".csv","",
list.files("path/to/files",full.names = FALSE),
fixed = TRUE)
# Now any of the files can be referred to by my_files[["filename"]],
# which really isn't much worse that just having separate filename variables in your workspace,
# and often it is much more convenient.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment