Created
February 25, 2021 14:26
Revisions
-
Aariq created this gist
Feb 25, 2021 .There are no files selected for viewing
This file contains 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ # Read in multiple .csv files with the same structure (same column names, numbers of columns, types of data in each column) and join them into a single data frame: library(readr) library(purrr) root <- "path/to/where/my/data/is/" file_paths <- list.files(root, pattern = "*.csv", full.names = TRUE) #set the names of the file_paths vector, here I'll just use the file names, but you can use whatever names(file_paths) <- list.files(root, pattern = "*.csv") #read them all and join them df <- map_df(file_paths, read_csv, .id = "filename") # Read in multiple sheets of a .xlsx file and join into a single dataframe, extracting the sheet names as a column. library(readxl) library(purrr) library(dplyr) my_wkbk <- "~/Documents/example.xlsx" #replace with path to your excel workbook. shts <- excel_sheets(my_wkbk) #gets sheet names df <- map(shts, ~read_excel(my_wkbk, sheet = .x)) %>% set_names(shts) %>% bind_rows(.id = "sheet_name") #or, if you don't care about extracting the sheet names and including them in your data frame... df <- map_df(shts, ~read_excel(my_wkbk, sheet = .x))