如何在R中导入具有多个工作表的多个Excel文件 [英] How to import multiple excel files with multiple sheet in r

查看:1377
本文介绍了如何在R中导入具有多个工作表的多个Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有60个Excel文件.每个文件有8张纸.每个文件的工作表相同,但每个文件的列数不同.

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.

我遇到了不同的帖子,这有助于我导入具有多个工作表的单个excel.但是我不想对60个文件重复此操作.有什么办法可以将所有工作表中的全部60个文件一起导入?

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?

我碰到过这篇文章 https://it.unt.edu/sites/default/files/importmultipleexcel_l_jds_aug2013. pdf . 但是他们在考虑同一列,并合并我不想要的所有内容.我只想导入.

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

推荐答案

您可以像下面这样使用函数read_excel_allsheets:

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)

这篇关于如何在R中导入具有多个工作表的多个Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆