简体   繁体   English

使用 R 中的一些工作表读取所有 Excel 文件

[英]Read all Excel files with some sheets in R

I have a very simple code:我有一个非常简单的代码:

data = read_excel("02.01.xlsx", col_names = F)

data <- data %>%
   mutate(direction = c(data[1,3])) %>% 
   tidyr::fill(1) %>% 
   slice(-1:-2) %>% 
   janitor::row_to_names(row_number = 1) %>% 
   purrr::set_names(c("date", "time", "max_price", "max_power", "nominal_power", "direction"))

But I must aplly it to every Excel file in my folder and some sheets(1,2,3,6,7,8,9,11)但我必须将它应用到我文件夹中的每个 Excel 文件和一些工作表(1、2、3、6、7、8、9、11)

I found this code:我找到了这个代码:

dir_path = "~/Documents/Dixi/Jan/"
re_file <- list.files(path = path, pattern = "*.xls")

read_sheets <- function(dir_path, file){
   xlsx_file <- paste0(dir_path, file)
   xlsx_file %>%
      excel_sheets() %>%
      set_names() %>%
      map_df(read_excel, path = xlsx_file, .id = 'sheet_name') %>% 
      mutate(file_name = file) %>% 
      select(file_name, sheet_name, everything())
}

df <- list.files(dir_path, re_file) %>% 
   map_df(~ read_sheets(dir_path, .))

but how to connect them?但如何连接它们? I'm new in purrr and it is very hard for me.我是 purrr 的新手,这对我来说很难。 Thanks!谢谢!

So in order to use the default parameters of read_excel you can do something as simple as that;所以为了使用 read_excel 的默认参数,你可以做一些简单的事情;

library(purrr)

dir_path = "~/Documents/Dixi/Jan/"
re_file <- list.files(path = dir_path, pattern = "*.xls")

# paste0(dir_path, "//", re_file) <- concatenate directory with file name
# readxl::read_excel <- reads data
map_df(paste0(dir_path, "//", re_file), readxl::read_excel)

However because you know your data better and apparently built a function to handle read_excel parameters, this should make your function work;但是,因为您更了解您的数据并且显然构建了一个函数来处理 read_excel 参数,所以这应该会使您的函数正常工作;

library(readxl)
library(purrr)

dir_path = "~/Documents/Dixi/Jan/"
re_file <- list.files(path = dir_path, pattern = "*.xls")

read_sheets <- function(dir_path, file){
  xlsx_file <- paste0(dir_path, file)
  xlsx_file %>%
    excel_sheets() %>%
    set_names() %>%
    map_df(read_excel, path = xlsx_file, .id = 'sheet_name') %>% 
    mutate(file_name = file) %>% 
    select(file_name, sheet_name, everything())
}

re_file %>%
  map_df(function(x) read_sheets(dir_path = dir_path, x))

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

相关问题 将多个 excel 文件中的所有工作表读入 R - Reading all sheets in multiple excel files into R R合并所有Excel文件中的所有图纸 - R to Merge All Sheets From All Excel Files R read_excel或readxl具有多个工作表的多个文件-绑定 - R read_excel or readxl Multiple Files with Multiple Sheets - Bind 使用 R 在文件夹中的所有 excel 文件(以及 excel 文件中的所有工作表)中写入新列 - Writing a new column in all excel files (and all sheets in an excel file) in a folder using R 循环读取和合并R中的多个Excel工作表 - Loop for Read and Merge multiple excel sheets in r 按表格将所有Excel文件读入R中,文件名作为列 - Read All Excel Files into R by Sheet with file name as column 无法读取R中的Excel工作表表格 - Can not read the tables of excel sheets in R 读取 excel 文件的文件夹并将单个工作表作为单独的 df 导入 R 中的名称 - Read a folder of excel files and import individual sheets as separate df's in R with Names 读取同一目录中的所有 Excel 工作簿,每个工作簿都有多个工作表,并将每个工作表导出为 R 中的 a.csv - Read all Excel workbooks in the same directory each with multiple sheets and export each sheet as a .csv in R 如何删除第一张表或读取第二张excel文件 - How to remove the first sheets or read the second sheets of the excel files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM