【问题标题】:how to combine multiple excel files having multiple worksheets into a single excel file with multiple worksheets in R如何在R中将具有多个工作表的多个excel文件组合成一个具有多个工作表的excel文件
【发布时间】:2019-09-21 13:46:04
【问题描述】:

我有多个文件名,例如 C1.xlsx; C2.xlsx; C3.xlsx; C4.xlsx 等,其中每个文件都有多个工作表,例如 C_1; C_2; C_3 等等......即所有文件中的工作表名称相同,并且所有文件中的工作表数量相等。 现在,我需要合并所有文件中具有相似工作表名称的所有工作表。另外,每个工作表中的列名都是一样的。

我正在使用以下代码读取文件夹中的所有 excel 文件

library(readxl)

files <- list.files(path = "~/Dropbox/Data/multiple_files", pattern = 
"*.xlsx", full.names = T)

tbl <- sapply(files, read_excel, simplify=FALSE) %>% 
bind_rows(.id = "id")

我拥有的excel文件如下所示:

C1.xlsx (worksheet:C_1)   (worksheet:C_2)   (worksheet:C_3)
A  B  C  D  E               A  B  C  D  E       A  B  C  D  E
1  4  6  8  C_1             2  4  6  1  C_2     1  4  6  8  C_3
3  56 7  8  C_1             2  3  6  8  C_2     2  3  5  6  C_3
2  4  6  1  C_1             7  8  3  4  C_2     3  4  6  7  C_3

C2.xlsx (worksheet:C_1)   (worksheet:C_2)   (worksheet:C_3)
A  B  C  D  E               A  B  C  D  E      A  B  C  D  E
3  7  1  3  C_1             1  4  7  1  C_2    1  9  6  1  C_3
1  6  9  2  C_1             2  3  6  8  C_2    2  3  5  6  C_3
2  4  6  1  C_1             7  1  3  4  C_2    3  4  2  7  C_3

C3.xlsx (worksheet:C_1)   (worksheet:C_2)   (worksheet:C_3)
A  B  C  D  E               A  B  C  D  E      A  B  C  D  E
9  4  6  8  C_1             1  4  6  1  C_2    1  4  1  1  C_3
3  5  7  1  C_1             1  3  6  4  C_2    2  1  5  1  C_3
2  7  6  1  C_1             7  7  3  4  C_2    3  4  6  7  C_3

excel文件中的组合工作表应如下所示:

Combined.xlsx (worksheet:C_1)   (worksheet:C_2)   (worksheet:C_3)
A  B  C  D  E               A  B  C  D  E        A  B  C  D  E
1  4  6  8  C_1             2  4  6  1  C_2      1  4  6  8  C_3
3  56 7  8  C_1             2  3  6  8  C_2      2  3  5  6  C_3
2  4  6  1  C_1             7  8  3  4  C_2      3  4  6  7  C_3
3  7  1  3  C_1             1  4  7  1  C_2      1  9  6  1  C_3
1  6  9  2  C_1             2  3  6  8  C_2      2  3  5  6  C_3
2  4  6  1  C_1             7  1  3  4  C_2      3  4  2  7  C_3
9  4  6  8  C_1             1  4  6  1  C_2      1  4  1  1  C_3
3  5  7  1  C_1             1  3  6  4  C_2      2  1  5  1  C_3
2  7  6  1  C_1             7  7  3  4  C_2      3  4  6  7  C_3

我正在寻找一个代码来首先读取文件夹中的所有 excel 文件,然后合并工作表

提前致谢

【问题讨论】:

  • 您将需要一个能够编写 xlsx 文件的包。我过去成功使用过openxlsx 包。一旦您可以读取文件并写入输出,您的问题就应该得到解决。

标签: r


【解决方案1】:

openxlsxwritexl 将起作用。我认为writexl 有点快。 来自文档:

将数据框写入 xlsx 文件。使用(多个)创建 xlsx 命名表,只需将 x 设置为数据框的命名列表。

以下代码“应该”工作:

spreadsheets <- purrr::map(files, function(.x){
  .spreadsheet <- purrr::map(1:3, .file = .x, function(.x, .file){
    .sheet <- readxl::read_xlsx(.file, sheet = .x)
  })
})
bound_ss <- purrr::map(1:3, .spreadsheet = spreadsheets, function(.x, .spreadsheet){
    do.call("rbind", purrr::map(.spreadsheet,.ind = .x, function(.x, .ind){
      purrr::pluck(.x, .ind)
      }))
})
# Should be a list of three sheets in the long format you expect
names(bound_ss) <- paste0("Sheet", 1:3)
  writexl::write_xlsx(bound_ss, path = "~/Dropbox/Data/multiple_files/sheet.xlsx")

## Sorting based on a column C1
library(magrittr)
bound_ss %<>% purrr::map(~ dplyr::arrange(.x, C1)) # ascending
bound_ss %<>% purrr::map(~ dplyr::arrange(.x, dplyr::desc(C1))) # descending

【讨论】:

  • 上述代码运行良好。但是,我想根据所有工作表中“E”列中的名称重命名工作表。还有....合并所有文件中的所有工作表后,我想根据特定列将数据框重新排列为升序或降序.....提前感谢
  • 我将工作表名称命名为如下 names(bound_ss)
  • @Kumar,如果您想根据列排序,可以使用以下代码,假设您的列名是 C1:library(magrittr)bound_ss %&lt;&gt;% purrr::map(~ dpyr::arrange(.x, C1) # for ascending orderbound_ss %&lt;&gt;% purrr::map(~ dpyr::arrange(.x, dplyr::desc(C1)) # for descending magrittr %&lt;&gt;% 是超级基本替代bound_ss &lt;- bound_ss %&gt;% 的有用原语。 Hadley Wickham 的 R for Data Science
  • 下面是不错的基础 R 解决方案!
【解决方案2】:

为了订购数据框列表,我使用了以下代码

sorted_bound_ss <- lapply(combined, function(C_1){
 C_1[order(C_1$A),]
})

成功了....

【讨论】:

    猜你喜欢
    • 2010-11-07
    • 2018-07-09
    • 2022-12-13
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 2021-01-09
    相关资源
    最近更新 更多