[英]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.