簡體   English   中英

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

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

我有一個非常簡單的代碼:

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"))

但我必須將它應用到我文件夾中的每個 Excel 文件和一些工作表(1、2、3、6、7、8、9、11)

我找到了這個代碼:

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, .))

但如何連接它們? 我是 purrr 的新手,這對我來說很難。 謝謝!

所以為了使用 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)

但是,因為您更了解您的數據並且顯然構建了一個函數來處理 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM