简体   繁体   English

将多个 excel 文件中的所有工作表读入 R

[英]Reading all sheets in multiple excel files into R

I am trying to read a bunch of excel files, and all of the sheets from these files into R.我正在尝试读取一堆 excel 文件,以及这些文件中的所有工作表到 R 中。 I would like to then save each sheet as a separate data frame with the name of the data frame the same name as the name of the sheet.然后我想将每个工作表保存为一个单独的数据框,数据框的名称与工作表的名称相同。 Some files only have 1 sheet, while others have more than one sheet so I'm not sure how to specify all sheets as opposed to just a number.有些文件只有一张,而其他文件不止一张,所以我不知道如何指定所有的工作表而不是一个数字。 I have tried:我努力了:

     library(XLConnect)   
     files.list <- list.files(recursive=T,pattern='*.xlsx')  #get files list from folder

     for (i in 1:length(files.list)){                                           
     wb <- loadWorkbook(files.list[i])           
     sheet <- getSheets(wb, sheet = )                      

     for (j in 1:length(sheet)){ 
         tmp<-read.xlsx(files.list[i], sheetIndex=j,
               sheetName=NULL,
               as.data.frame=TRUE, header=F)   
    if (i==1&j==1) dataset<-tmp else dataset<-rbind(dataset,tmp)   

      }
    }

and I get an error "could not find function "loadWorkbook"".我收到一个错误“找不到 function “loadWorkbook””。 At one point I resolved that issue and got an error "could not find function "getSheets"".在某一时刻,我解决了该问题并收到错误“找不到 function “getSheets””。 I have had some issues getting this package to work so if anyone has a different alternative I would appreciate it!我在让这个 package 工作时遇到了一些问题,所以如果有人有不同的选择,我将不胜感激!

I'm pretty sure, the loadWorkbook function comes from package openxlsx .我很确定, loadWorkbook function 来自 package openxlsx So you should use:所以你应该使用:

library(openxlsx)

https://cran.r-project.org/web/packages/openxlsx/openxlsx.pdf https://cran.r-project.org/web/packages/openxlsx/openxlsx.pdf

You could try with readxl ...您可以尝试使用readxl ...

I've not tested this for the case of different workbooks with duplicate worksheet names.对于具有重复工作表名称的不同工作簿,我没有对此进行测试。

There were a number of issues with your code:您的代码存在许多问题:

  1. the list.files pattern included a. list.files 模式包括一个。 which is a reserved character so needs to be escaped with the \\这是一个保留字符,因此需要使用\\进行转义
  2. As @deschen pointed out the excel referring functions are from the openxlsx package正如@deschen 所指出的,excel 引用函数来自openxlsx package
library(readxl)

files.list <- list.files(recursive = T, pattern = '*\\.xlsx')  #get files list from folder

for (i in seq_along(files.list)){
  
  sheet_nm <- excel_sheets(files.list[i])

    for (j in seq_along(sheet_nm)){
  
      assign(x = sheet_nm[j], value = read_xlsx(path = files.list[i], sheet = sheet_nm[j]), envir = .GlobalEnv)
  }

}

Created on 2022-01-31 by the reprex package (v2.0.1)代表 package (v2.0.1) 于 2022 年 1 月 31 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM