简体   繁体   English

R:使用正则表达式从多个 Excel 文件中导入特定工作表

[英]R: Use Regex to Import Specific Sheets from Multiple Excel Files

I have a group of Excel files with multiple sheets which don't follow a standard naming convention.我有一组 Excel 文件,其中包含多个不遵循标准命名约定的工作表。 I want to create a single data frame from specific sheets containing the keyword 'frame' .我想从包含关键字'frame' 的特定工作表中创建一个数据框。

library(tidyverse)
library(openxlsx)

# Sample Excel File 1
df1 <- data.frame(replicate(10,sample(0:1,10,rep=TRUE)))
data_frame2 <- data.frame(replicate(10,sample(0:1,10,rep=TRUE)))
list_of_datasets1 <- list("df" = df1, "date_frame" = data_frame2)
write.xlsx(list_of_datasets1, file = "writeXLSX1.xlsx")

# Sample Excel File 2
df3 <- data.frame(replicate(10,sample(0:1,10,rep=TRUE)))
data_frame4 <- data.frame(replicate(10,sample(0:1,10,rep=TRUE)))
list_of_datasets2 <- list("date_frames" = df3, "dfs" = data_frame4)
write.xlsx(list_of_datasets2, file = "writeXLSX2.xlsx")

# Create List of Excel Files
excel_file_list <- list.files(pattern = "writeXLSX\\d*.xlsx", full.names = T)

I'd like to be able to do this using a regex with purr like this:我希望能够使用带有purr的正则表达式来做到这一点,如下所示:

df_bind <- excel_file_list %>%
  map_dfr(~read_excel(.x, sheet = grepl("frame", .x)))

The closest answer I found works fine with a single file.我找到的最接近的答案适用于单个文件。 However, I can't quite figure out how to extract the sheet names correctly when they're in a list.但是,当它们在列表中时,我不太清楚如何正确提取工作表名称。

We can use str_detect我们可以使用str_detect

library(readxl)
library(dplyr)
library(purrr)
excel_file_list %>% 
      map_dfr(~ read_excel(.x, sheet = which(str_detect(excel_sheets(.x), 'frame'))))

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

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