Skip to content

Instantly share code, notes, and snippets.

@mhweber
Created February 24, 2023 19:26
Show Gist options
  • Save mhweber/59786d3f4ce117ef9bc22127ffd61a32 to your computer and use it in GitHub Desktop.
Save mhweber/59786d3f4ce117ef9bc22127ffd61a32 to your computer and use it in GitHub Desktop.
Combine multiple .csv files in R
require(readr) # for read_csv()
require(dplyr) # for mutate()
require(tidyr) # for unnest()
require(purrr) # for map(), reduce()
data_path <- 'c:/user/myname' # put your file path here
# find all file names ending in .csv
files <- dir(data_path, pattern = "*.csv")
data <- files %>%
# read in all the files, appending the path before the filename
map(~ read_csv(file.path(data_path, .))) %>%
reduce(rbind)
# Write out full csv file
write_csv(data, paste0(data_path,'final_data.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment