# This is accompanying code for # https://jozef.io/r005-import-csvs/ # Original inspiration of the post: # https://www.gerkelab.com/blog/2018/09/import-directory-csv-purrr-readr/ # Download and prepare data =================================================== dlUrl <- "https://www.gerkelab.com/data/ie-general-referrals-by-hospital.zip" download.file(dlUrl, destfile = "~/ie-general-referrals-by-hospital.zip") unzip("~/ie-general-referrals-by-hospital.zip", exdir = "~") # Quick import of all csvs with base R ======================================== data_dir <- "~/ie-general-referrals-by-hospital" filePaths <- list.files(data_dir, "\\.csv$", full.names = TRUE) result <- do.call(rbind, lapply(filePaths, read.csv)) # View part of the result head(result) # Reconstructing the results of the original post ============================= filePaths <- list.files(data_dir, "\\.csv$", full.names = TRUE) result <- do.call(rbind, lapply(filePaths, function(path) { df <- read.csv(path, stringsAsFactors = FALSE) df[["source"]] <- rep(path, nrow(df)) df[["Month_Year"]] <- as.Date( paste0(sub("-20", "-", df[["Month_Year"]], fixed = TRUE), "-01"), format = "%b-%y-%d" ) df })) # View part of the result head(result) # Alternatives to Base R - data.table ========================================= library(data.table) filePaths <- list.files(data_dir, "\\.csv$", full.names = TRUE) result <- lapply(filePaths, fread) names(result) <- filePaths result <- rbindlist(result, use.names = TRUE, idcol = "source") result[, Month_Year := as.Date( paste0(sub("-20", "-", Month_Year, fixed = TRUE), "-01"), format = "%b-%y-%d" )] # View part of the result head(result) # Cleanup ===================================================================== unlink("~/ie-general-referrals-by-hospital.zip") unlink("~/ie-general-referrals-by-hospital", recursive = TRUE)