R 根据 sheet 名称和列名称连接 Excel 文件中的列

R Concatenate Columns from Excel file based on sheet name and Column's name

大家好,我有一个 excel 文件,其中有多个 sheetnames,这些 sheet 名称并不总是具有相同的结构,我希望能够阅读 excel文件,只读取一些细节 sheets,select 一些特定的列,然后创建一个串联的 table 我有以下 excel 文件:

File_Mother.xlsx 它有 5 个 sheet,但我们只需要那些 sheet 在其名称开头具有模式“inning_19”的 sheet,因此“ inning_195" 和 "inning_1988" 如果它们存在于 excel 文件中,将很有趣,提取列 ID_MATCHSET_GAME 以及所有其他列以模式“id_complete_game_on”开头的列,最后创建一个列,让我知道当我们 bind 垂直所有数据 [=53] 时获取此信息的 sheet 名称=]编辑。例如:

来自File_Mother.xlsx 我有 sheet.name = "inning_195" 具有以下信息

ID_MATCH SET_GAME id_complete_game_on_field id_complete_game_on_camp
X01 M cleared not cleared
X85 Q cleared cleared
X89 U cleared cleared

来自File_Mother.xlsx 我有 sheet.name = "inning_1988" 具有以下信息

ID_MATCH SET_GAME id_complete_game_on_demand_one id_complete_game_on_camp
IKX01 MR completed not cleared
IKX02 DD not completed cleared
IKX03 TT draw cleared

经过所需的数据整理后,输出将是

ID_MATCH SET_GAME id_complete_game_on_field id_complete_game_on_camp id_complete_game_on_demand_one sheet name
X01 M cleared not cleared NA inning_195
X85 Q cleared cleared NA inning_195
X89 U cleared cleared NA inning_195
IKX01 MR NA NA completed inning_1988
IKX02 DD NA NA not completed inning_1988
IKX03 TT NA NA draw inning_1988

非常感谢你们,我整个星期都在努力做到这一点,但我还没有得到我想要的,如果有博客或一段代码,你认为可以帮助 post关于评论非常感谢!我会不断寻找答案,再次感谢您

我稍微编辑了@Jeromy Anglim 的 reading all sheets of excel

library(dplyr)
library(purrr)

read_excel_somesheets <- function(filename, key,tibble = FALSE) {
  sheets <- readxl::excel_sheets(filename)
  x <- lapply(sheets, function(X) readxl::read_excel(filename, sheet = X) %>% mutate(sheet_name = X))
  if(!tibble) x <- lapply(x, as.data.frame)
  names(x) <- sheets
  y <- x[grepl(key, names(x))] %>%
    reduce(full_join)
  y
}

那你可以试试

read_excel_somesheets("your_path/File_Mother.xlsx", "inning_19")

结果就像

  ID_MATCH SET_GAME id_complete_game_on_field id_complete_game_on_camp  sheet_name id_complete_game_on_demand_one
1      X01        M                   cleared              not cleared  inning_195                           <NA>
2      X85        Q                   cleared                  cleared  inning_195                           <NA>
3      X89        U                   cleared                  cleared  inning_195                           <NA>
4    IKX01       MR                      <NA>              not cleared inning_1988                      completed
5    IKX02       DD                      <NA>                  cleared inning_1988                  not completed
6    IKX03       TT                      <NA>                  cleared inning_1988                           draw