将多个xl文件读入数据框
我一直在使用XLConnect
函数loadworkbook
将每个xlsx文件加载到R然后rbind
将它们合并在一起。 这样做的最好方法是什么,而不是写多个DF来稍后合并它们。 我正在尝试使用下面的代码将我的Excel文件合并到2个数据框(大多数文件的2个工作表名称)。 列总是相同的,但文件名将会改变。
当前/缓慢的方式
require(XLConnect) df <- loadWorkbook(paste(location,'UK.xlsx',sep="")) dfb <- loadWorkbook(paste(location,'US.xlsx',sep="")) UK <-readWorksheet(df,sheet="School",startRow=0,startCol=0,autofitRow=TRUE,endCol=21,header=TRUE) US <-readWorksheet(dfb,sheet="School",startRow=0,startCol=0,autofitRow=TRUE,endCol=21,header=TRUE) School <- rbind(UK,US) UK <-readWorksheet(df,sheet="College",startRow=0,startCol=0,autofitRow=TRUE,endCol=21,header=TRUE) US <-readWorksheet(dfb,sheet="College",startRow=0,startCol=0,autofitRow=TRUE,endCol=21,header=TRUE) College <- rbind(UK,US)
新的代码
require(readxl) filelist<- list.files(location,pattern='xlsx',full.names = T)
如果不是每个文件都具有sheetname,我怎样才能将每个sheetname读入数据框。 我需要2个数据框1为学校和1为学院。 我想我需要尝试像Schools <-lapply(filelist, read_excel, sheet="School")
但我得到错误:表'学校'找不到。 我认为这个错误是因为表单学校不在每个文件上。 我正在使用list.files
因为文件名不总是相同的。
这个方法呢?
library(purrr) library(readxl) # filenames to xl-sheets files <- sprintf("Mappe%i.xlsx", 1:3) # read only df for xl-files with school-sheet xl_school <- map_if(files, ~ "School" %in% excel_sheets(.x), ~read_excel(.x)) # read only df for xl-files with college-sheet xl_college <- map_if(files, ~ "College" %in% excel_sheets(.x), ~read_excel(.x)) # combine school-files to data frame (repeat same for college) school_df <- map_df(xl_school, function(x) if(is.data.frame(x)) x) school_df #> # A tibble: 3 × 1 #> Test #> <chr> #> 1 fdsf #> 2 543534 #> 3 gfdgfdd
您可能需要强制列types为文本。 只需将col_types = "text"
添加到read_excel()
:
# read only df for xl-files with school-sheet xl_school <- map_if(files, ~ "School" %in% excel_sheets(.x), ~read_excel(.x, col_types = "text")) # read only df for xl-files with college-sheet xl_college <- map_if(files, ~ "College" %in% excel_sheets(.x), ~read_excel(.x, col_types = "text"))