【问题标题】:Read only selective Excel workbooks into R and save them as dataframe仅将选择性 Excel 工作簿读入 R 并将它们保存为数据框
【发布时间】:2021-05-05 13:26:08
【问题描述】:

我有一个包含 3 张工作表(工作表名称 = vi、hi 和 hh)的 Excel 工作簿,我只想导入工作表 hi 和 hh 并将它们存储为数据框。

这个 sn-p 加载 3 张作为列表

library(readxl)
library(tidyverse)

my_path <-  "my_file.xlsx"

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

但我只想导入工作表 2 和 3 以及未列出的数据框,所以我尝试了这个,但它返回空 tibble。这里缺少什么?

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

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

【问题讨论】:

    标签: r tidyverse purrr readxl


    【解决方案1】:

    您可以将工作表名称传递给read_excel

    my_path <-  "my_file.xlsx"
    patterns <- c( "hi" ,   "hh")
    
    result <- map_df(patterns, ~read_excel(path = my_path, sheet = .x))
    

    【讨论】:

      猜你喜欢
      • 2022-10-30
      • 2021-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 1970-01-01
      相关资源
      最近更新 更多