Skip to content

Instantly share code, notes, and snippets.

@Aariq
Created February 25, 2021 14:26

Revisions

  1. Aariq created this gist Feb 25, 2021.
    28 changes: 28 additions & 0 deletions read-multiple.R
    Original 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))