Sumaiya Khatun
Sumaiya Khatun

Reputation: 57

How to import multiple excel files with multiple sheet in r

I have 60 Excel files. Each files have 8 sheets. Sheets are same for each files but number of columns are different for each file.

I have come across different post which helped me to import a single excel with a multiple sheet. But I do not want to repeat this for 60 files . Is there any way to import all 60 files with all sheets together?

I have came across this article https://it.unt.edu/sites/default/files/importmultipleexcel_l_jds_aug2013.pdf. But there they are considering same columns and merging everything which I do not want. I just want to import.

EDIT.
The function in the comment is this:

read_excel_allsheets <- function(filename) {
    sheets <- readxl::excel_sheets(filename)
    x <- lapply(sheets, function(X) readxl::read_excel(filename, sheet = X))
    names(x) <- sheets
    x
}


mysheets <- read_excel_allsheets("ALL_1_18_2018.xlsx")

Upvotes: 1

Views: 2415

Answers (1)

Ralf Stubner
Ralf Stubner

Reputation: 26823

You can use your function read_excel_allsheets like this:

read_excel_allsheets <- function(filename) { 
  sheets <- readxl::excel_sheets(filename) 
  x <- lapply(sheets, function(X) readxl::read_excel(filename, sheet = X)) 
  names(x) <- sheets 
  x 
} 
files <- list.files(path = "/directory/in/question/", 
                     pattern = "*.xlsx",
                     full.names = TRUE)
out <- lapply(files, read_excel_allsheets)
names(out) <- basename(files)

Upvotes: 2

Related Questions