Created
March 18, 2016 14:50
-
-
Save apreshill/1d197a3403aa4483454f to your computer and use it in GitHub Desktop.
Read multiple csv files into R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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