我使用openxlsx::read.xlsx
最后一次我需要从XLSX读取许多表.
#install.packages("openxlsx")
library(openxlsx)
#?openxlsx::read.xlsx
#using file chooser:
filename <- file.choose()
#or hard coded file name:
#filename <- "filename.xlsx"
#get all the sheet names from the workbook
SheetNames<-getSheetNames(filename)
# loop through each sheet in the workbook
for (i in SheetNames){
#Read the i'th sheet
tmp_sheet<-openxlsx::read.xlsx(filename, i)
#if the input file exists, append the new data;; else use the first sheet to initialize the input file
ifelse(exists("input"),
input<-rbind(input, tmp_sheet),
input<-tmp_sheet)
}
注意:这假设每个工作表具有相同的列结构和数据类型.您可能需要将数据标准化(例如,tmp_sheet <- as.data.frame(sapply(tmp_sheet,as.character), stringsAsFactors=FALSE)
),或者在合并之前将每个工作表加载到自己的数据帧和预处理中.