Skip to content

Instantly share code, notes, and snippets.

@narath
Created March 21, 2019 19:22
Show Gist options
  • Save narath/9c5d09e5681e741999d253235849d8d3 to your computer and use it in GitHub Desktop.
Save narath/9c5d09e5681e741999d253235849d8d3 to your computer and use it in GitHub Desktop.
R code to read all the csv files into a single dataset (only works when all csv files have the same format)
read_all_csv_files <- function(dir) {
save_dir <- getwd()
setwd(dir)
files <- list.files(pattern = "csv")
for (file in files) {
if (!exists("dataset")){
dataset <- read.csv(file)
} else {
temp_dataset <- read.csv(file, stringsAsFactors = FALSE)
dataset <- rbind(dataset, temp_dataset)
rm(temp_dataset)
}
}
setwd(save_dir)
return(dataset)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment