【问题标题】:Reading multiple Excel files (with multiple sheets) in a directory in R在R中的目录中读取多个Excel文件(带有多个工作表)
【发布时间】:2021-01-01 06:51:56
【问题描述】:

我有多个 .xlsx 文件,我想阅读这些文件并将它们合并到一个文件中。但是,每个文件都包含两张纸,所以我想要一个文件全部是表格 1,然后 1 个文件全部是表格 2。

我之前使用过这样的代码来读取多个文件,但它没有考虑不同的工作表。

files = list.files(path = "../input_data/", 
                         pattern = "*.xlsx", 
                         full.names = T)
  
combined_data = sapply(files, read_excel, simplify = F) %>% 
    rbind.fill()

我曾尝试将sheet 参数添加到read_excel 函数,但没有奏效。有任何想法吗?谢谢!

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    我推荐使用最现代的openxlsx 包:

    df1 <- purrr::map_dfr(
      files,
      function(x) {
        openxlsx::read.xlsx(x, sheet = 1)
      }
    )
    df2 <- purrr::map_dfr(
      files,
      function(x) {
        openxlsx::read.xlsx(x, sheet = 2)
      }
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-14
      • 2018-07-09
      • 2021-11-22
      相关资源
      最近更新 更多