将具有多个工作表的多个xlsx文件读入一个R数据框

问题描述 投票:6回答:4

我一直在阅读如何阅读和组合多个xlsx。文件到一个R数据框,并遇到一些非常好的建议,如How to read multiple xlsx file in R using loop with specific rows and columns,但到目前为止不适合我的数据集。

我希望R读取多个xlsx文件,其中包含多个工作表。所有工作表和文件具有相同的列但长度不同,应排除NA。我想跳过前3行,只考虑第1:6,8:10,12:17,19列。

到目前为止我试过:

file.list <- list.files(recursive=T,pattern='*.xlsx')

dat = lapply(file.list, function(i){
    x = read.xlsx(i, sheetIndex=1, sheetName=NULL, startRow=4,
              endRow=NULL, as.data.frame=TRUE, header=F)
# Column select 
    x = x[, c(1:6,8:10,12:17,19)]
# Create column with file name  
    x$file = i
# Return data
    x
  })

  dat = do.call("rbind.data.frame", dat)

但这只占用了每个文件的第一张

有谁知道如何在一个R数据框中将所有工作表和文件放在一起?

另外,您会推荐哪些包用于大型数据集?到目前为止,我尝试了readxl和XLConnect。

太感谢了!

r excel xlsx readxl
4个回答
2
投票

我会使用这样的嵌套循环来浏览每个文件的每个表格。它可能不是最快的解决方案,但它是最简单的。

require(xlsx)    
file.list <- list.files(recursive=T,pattern='*.xlsx')  #get files list from folder

for (i in 1:length(files.list)){                                           
  wb <- loadWorkbook(files.list[i])           #select a file & load workbook
  sheet <- getSheets(wb)                      #get sheet list

  for (j in 1:length(sheet)){ 
    tmp<-read.xlsx(files.list[i], sheetIndex=j, colIndex= c(1:6,8:10,12:17,19),
                   sheetName=NULL, startRow=4, endRow=NULL,
                   as.data.frame=TRUE, header=F)   
    if (i==1&j==1) dataset<-tmp else dataset<-rbind(dataset,tmp)   #happend to previous

  }
}

您可以在加载阶段后清除NA值。


4
投票

openxlsx解决方案:

filename <-"myFilePath"

sheets <- openxlsx::getSheetNames(filename)
SheetList <- lapply(sheets,openxlsx::read.xlsx,xlsxFile=filename)
names(SheetList) <- sheets

3
投票

这是一个tidyversereadxl驱动选项,它返回一个数据框,其中包含每个文件的文件和工作表名称列。

在此示例中,并非每个文件都具有相同的工作表或列; test2.xlsx只有一个工作表,test3.xlsx工作表1没有col3。

library(tidyverse)
library(readxl)

dir_path <- "~/test_dir/"         # target directory path where the xlsx files are located. 
re_file <- "^test[0-9]\\.xlsx"    # regex pattern to match the file name format, in this case 'test1.xlsx', 'test2.xlsx' etc, but could simply be 'xlsx'.

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, .))

# A tibble: 15 x 5
   file_name  sheet_name  col1  col2  col3
   <chr>      <chr>      <dbl> <dbl> <dbl>
 1 test1.xlsx Sheet1         1     2     4
 2 test1.xlsx Sheet1         3     2     3
 3 test1.xlsx Sheet1         2     4     4
 4 test1.xlsx Sheet2         3     3     1
 5 test1.xlsx Sheet2         2     2     2
 6 test1.xlsx Sheet2         4     3     4
 7 test2.xlsx Sheet1         1     3     5
 8 test2.xlsx Sheet1         4     4     3
 9 test2.xlsx Sheet1         1     2     2
10 test3.xlsx Sheet1         3     9    NA
11 test3.xlsx Sheet1         4     7    NA
12 test3.xlsx Sheet1         5     3    NA
13 test3.xlsx Sheet2         1     3     4
14 test3.xlsx Sheet2         2     5     9
15 test3.xlsx Sheet2         4     3     1

0
投票

这个“rio”包的另一个解决方案:

library("rio")

# import and rbind all worksheets
DT <- import_list(SINGLE_XLSX_PATH, rbind = TRUE)

来源:rdrr.io

© www.soinside.com 2019 - 2024. All rights reserved.