How to import multiple excel files with multiple sheet in r

二次信任 提交于 2019-12-01 11:42:50

问题


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")

回答1:


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)


来源:https://stackoverflow.com/questions/48519464/how-to-import-multiple-excel-files-with-multiple-sheet-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!