Skip to content

Instantly share code, notes, and snippets.

@cgpu
Forked from apreshill/read-multiple-csv-files
Created April 29, 2020 22:31
Show Gist options
  • Save cgpu/b1d9d423c312caa65e67ed29193aa94c to your computer and use it in GitHub Desktop.
Save cgpu/b1d9d423c312caa65e67ed29193aa94c 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