【问题标题】:Reading multiple xl files into dataframe将多个excel文件读入数据框
【发布时间】:2017-05-11 11:51:29
【问题描述】:

我一直在使用 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) 当不是每个文件都有两个工作表名时,如何将每个工作表名读入数据框中。我需要 2 个数据框,1 个用于学校,1 个用于大学。 我想我需要尝试Schools &lt;-lapply(filelist, read_excel, sheet="School") 之类的东西,但我得到错误:找不到工作表“学校”。我认为这个错误是因为工作表学校不在每个文件上。我正在使用list.files,因为文件名并不总是相同。

【问题讨论】:

  • 您应该通读包中的可用函数,看看是否有将工作表名称作为向量提供的函数。如果是,那么您可以检查床单是否存在。例如,openxlsx 包具有用于此目的的函数 getSheetNames
  • 工作表名称总是相同的学校和学院,但并非每个文件都有,这就是为什么我不知道如何将它们合并在一起。
  • 我需要一个数据框,它将为每个文件合并每个名为 School 的工作表名称,并为 College 做同样的事情,但我不知道这样做的最佳方法是什么。谢谢
  • 您可以尝试获取工作表 School,如果失败则捕获错误,然后优雅地继续前进。你可以使用tryCatch 来做到这一点。

标签: r excel xlconnect


【解决方案1】:

这种方法怎么样?

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

您可能需要强制列类型为文本。只需将col_types = "text" 添加到read_excel()-call:

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

【讨论】:

  • 很高兴我能帮上忙。但是……呜呜?包名purrr,CRAN/R区分大小写。
  • 这正在工作,但现在在我的大学工作表上它返回了相同的结果,我刚刚在一些旧数据上进行了测试,我收到以下错误,期望数字:这与文本 col 相关。
  • 所以有时(即在某些烟雾中)列包含数值,有时包含文本?如果是这样,您可以在调用 map_df 之前转换为字符:xl_school &lt;- map(xl_school, ~as.character(.x)) - 我还不能检查,因为我是用手机写的。
  • 感谢您的帮助,我无法在 map_df 之前转换为字符,因为我收到了 map_if 的错误。使用你的第二个选项是我目前有 xl_school &lt;- map_if(files, ~ "School" %in% excel_sheets(.x), col_types = "text",~read_excel(.x)) 位我得到未使用的参数 (col_types = "text")
  • 如果我尝试 xl_school &lt;- map_if(files, ~ "School" %in% excel_sheets(.x), ~read_excel(col_types = "text",.x)) 我得到错误:每列需要一个名称和类型
猜你喜欢
  • 2019-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-22
  • 2016-02-16
  • 2019-11-25
相关资源
最近更新 更多