简体   繁体   中英

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)

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. Thanks!

So in order to use the default parameters of read_excel you can do something as simple as that;

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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