r - 如何使用多张工作表导入多个 excel 文件?
问题描述
大家晚安:
我正在尝试导入 8 个结构相同但名称不同的 excel 文件。我有使用前 3 张纸导入一个文件的代码,但我必须重复此代码 8 次。我想创建一个函数只放一次,而且我需要知道它们来自哪里(excel文件的名称)。先感谢您。
library(readxl)
read_excel_allsheets <- function("path", tibble = false){
sheets <- readxl::excel_sheets("path")
sheets <- sheets[c(1,2,3)]
x <- lapply(sheets, function(X) readxl::read_excel("path", sheet = X,skip = 5))
if(!tibble) x <- lapply(x, as.data.frame)
names(x) <- sheets
x
all_data1 <- do.call(rbind, lapply(sheets, function(X)
transform(readxl::read_excel("path", sheet = X,skip = 5), estatus = X,tipo="Corriente",enfoque="Sector")))
}
解决方案
尝试使用这个:
read_excel_allsheets <- function(path, tibble = FALSE) {
sheets <- readxl::excel_sheets(path)
sheets <- sheets[c(1,2,3)]
x <- do.call(rbind, lapply(sheets, function(X)
transform(readxl::read_excel(path, sheet = X,skip = 5),
estatus = X,tipo="Corriente",enfoque="Sector")))
if(!tibble) x <- lapply(x, as.data.frame)
names(x) <- sheets
return(x)
}
all_data <- lapply(list.files('/path', full.names = TRUE), read_excel_allsheets)
推荐阅读
- command-line - 如何使用 Cyrillic 字体列出文件夹的内容?
- javascript - 如何在 Matlab/Python/JS 中获取形状(/Body)的 X、Y(和 Z)坐标?
- cmake - 如何将没有价值的参数传递给 CMake?
- r - 这是 R 神经网络过拟合吗?
- c# - 特定字符之前的 C# 文本
- java - 将方法的返回值作为参数传递给同一类中的另一个方法
- ios - Swift 4 - PDFAnnotation 文本(或 UITextField)是文本的大小
- android - 恢复旧应用的应用id(Eclipse时代)
- android - 无法解析符号“LocalBroadcastManager”
- ios - 在 iOS PWA 中从 jsPDF 下载 PDF