只将选择性 Excel 工作簿读入 R 并将它们保存为数据框

Read only selective Excel workbooks into R and save them as dataframe

我有一个 Excel 工作簿,其中包含 3 sheets (sheets name = vi , hi 和 hh) ,我只想导入 sheet hi 和hh 并将它们存储为数据框。

此代码段加载 3 sheet 秒作为列表

library(readxl)
library(tidyverse)

my_path <-  "my_file.xlsx"

my_path %>% 
  excel_sheets() %>% 
  set_names() %>% 
  map(read_excel, path = my_path) 

但我只想导入 sheet 2 和 3 以及未列出的数据框,所以我尝试了这个,但它 returns 是空的。这里缺少什么?

patterns <- c( "hi" ,   "hh")

my_path %>% 
  excel_sheets() %>% 
  set_names() %>% 
  map_dfr(patterns, ~read_excel(path = my_path))

您可以将 sheet 名称传递给 read_excel

my_path <-  "my_file.xlsx"
patterns <- c( "hi" ,   "hh")

result <- map_df(patterns, ~read_excel(path = my_path, sheet = .x))