Skip to content

Instantly share code, notes, and snippets.

@adelavega
Created December 3, 2013 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adelavega/7778752 to your computer and use it in GitHub Desktop.
Save adelavega/7778752 to your computer and use it in GitHub Desktop.
R function that loads all CSVs in a given directory
# Loads all CSV files into a data set in given INPUT_PATH
loadAllCSVs <- function (INPUT_PATH) {
# Get list of files
file_list <- list.files(INPUT_PATH)
# Iterate through files and add them to "dataset"
for (file in file_list){
file <- paste(INPUT_PATH,file,sep="")
# if the merged dataset doesn't exist, create it
if (!exists("dataset")){
dataset <- read.table(file, header=TRUE, sep=",")
}
# if the merged dataset does exist, append to it
if (exists("dataset")){
temp_dataset <-read.table(file, header=TRUE, sep=",")
dataset<-rbind(dataset, temp_dataset)
rm(temp_dataset)
}
}
return(dataset)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment