Marie
Marie

Reputation: 55

Import sheets from Excel files located in different folder in R

Basically I have two Excel files with the same name "Checklist" in two different folder (one is 2018 and the other one is 2019). Checklist has different sheets, one for each month : "January", "February" etc... Of course, all the sheets have exactly the same variables. I would like to put in the same data frame all the sheets from both Excel files. For now, I can gather the sheets from one Excel File with :

library(readxl)   
library(tibble)
read_excel_allsheets <- function(filename, tibble = TRUE) {
  sheets <- readxl::excel_sheets(filename)
  x <- lapply(sheets, function(X) readxl::read_excel(filename, sheet = X))
  if(!tibble) x <- lapply(x, as.data.frame)
  names(x) <- sheets
  x
}

mysheets <-read_excel_allsheets("C:/Users/Thiphaine/Documents/2018/Checklist.xlsx")
library(dplyr)
mysheets<-bind_rows(mysheets, .id = "column_label")

I just don't know how to create a loop that will go through the folder 2018 and 2019 to gather all the sheets from both Excel file. The idea will also be that in 2020, I will have another folder "2020" that should be included... Any idea? Thanks

Upvotes: 0

Views: 1854

Answers (2)

alko989
alko989

Reputation: 7938

Try this:

library(dplyr)
allsheets <- list()
for(file in list.files(path = "PATH/TO/DCUMENTS/", 
    recursive = TRUE, pattern = "*.xlsx", full.names = TRUE)) {
  mysheets <- read_excel_allsheets(file)
  mysheets <- bind_rows(mysheets, .id = "column_label") 
  allsheets[[file]] <- mysheets
}

where PATH/TO/DOCUMENTS is probably something like "C:/Users/Thiphaine/Documents/ for you.

Upvotes: 1

DJV
DJV

Reputation: 4873

If you'd like can also vectorize it using the tidyverse approach. Especially because all of your files are the same (column names) and you want to end up with a data.frame.

require(tidyverse)

df <- list.files(path = "your_path",
                       full.names = TRUE,
                       recursive = TRUE,
                       pattern = "*.xls") %>% 
tbl_df() %>%
mutate(sheetName = map(value, readxl::excel_sheets)) %>%
unnest(sheetName) %>% 
mutate(myFiles = purrr::map2(value, sheetName, function(x,y) {
    readxl::read_excel(x, sheet = paste(y))})) %>% 
unnest(myFiles)

Upvotes: 0

Related Questions