Created
February 24, 2023 19:26
-
-
Save mhweber/59786d3f4ce117ef9bc22127ffd61a32 to your computer and use it in GitHub Desktop.
Combine multiple .csv files in R
This file contains hidden or 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
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