简体   繁体   English

导入具有多个工作表的Excel工作簿

[英]Import excel workbook with multiple sheets

I am looking to import an excel workbook into R with multiple sheets. 我想将具有多个工作表的excel工作簿导入到R中。 However, I can't seem to quite make this work. 但是,我似乎不太能完成这项工作。 The code I have been using is the following: 我一直在使用的代码如下:

library(XLConnect)
# Read Excel Sheet
excel <- loadWorkbook("C:/Users/rawlingsd/Downloads/17-18 Prem Stats.xlsx")
# get sheet names
sheet_names <- getSheets(excel)
names(sheet_names) <- sheet_names
# put sheets into a list of data frames
sheet_list <- lapply(sheet_names, function(.sheet){readWorksheet(object=excel, .sheet)})
# limit sheet_list to sheets with at least 1 dimension 
# sheet_list2 <- sheet_list[sapply(sheet_list, function(x) dim(x)[1]) > 0]
# code to read in each excel worksheet as individual dataframes
for (i in 1:length(sheet_list)){assign(paste0("2018df", i), as.data.frame(sheet_list[i]))}
# define function to clean data in each data frame (updated based on your data)

If anyone could help me with my code or share a code that works for them, it would be greatly appreciated 如果有人可以帮助我修改我的代码或共享适用于他们的代码,将不胜感激

You can use readxl package. 您可以使用readxl软件包。 See the following example. 请参见以下示例。

library(readxl)
path <- readxl_example("datasets.xls")
sheetnames <- excel_sheets(path)
mylist <- lapply(excel_sheets(path), read_excel, path = path)

# name the dataframes
names(mylist) <- sheetnames

The spreadsheet will be captured in a list with the sheetname as the name of the dataframe in the list. 电子表格将被捕获在一个列表中,其中工作表名称为列表中数据框的名称。

If you want to bring the dataframes out of the list use the next bit of code. 如果要将数据框排除在列表外,请使用下一部分代码。

# Bring the dataframes to the global environment
list2env(mylist ,.GlobalEnv)

What I use: 我用什么:

full_excel_read<-function(fpath,v=TRUE){



 sheetnames <- excel_sheets(fpath)
  workbook <- sapply(sheetnames,function (x){readxl::read_excel(fpath,sheet = x)})
  for (sh in sheetnames) {
    workbook[[sh]]<-as.data.table(workbook[[sh]])
  }
  if (v){
    lapply(sheetnames, function(x){View(workbook[[x]],x)})
  }


  workbook
}

Please look into openxlsx package, which allows you to do loads of stuff with excel workbooks. 请查看openxlsx软件包,该软件包可让您使用excel工作簿执行大量工作。 Here is a code script to read all the sheets from a given workbook. 这是一个代码脚本,用于读取给定工作簿中的所有工作表。

library(openxlsx)
a <- loadWorkbook('~/filename.xlsx')
sheetNames <- sheets(a)
for(i in 1:length(sheetNames))
{
  assign(sheetNames[i],readWorkbook(a,sheet = i))
}

You can verify the data is loaded in R and can view in your workSpace. 您可以验证数据已加载到R中并可以在工作空间中查看。

Thanks. 谢谢。

See Read all worksheets in an Excel workbook into an R list with data.frames 参阅将Excel工作簿中的所有工作表读入带有data.frames的R列表中

require(XLConnect)
wb <- loadWorkbook(system.file("demoFiles/mtcars.xlsx", package = "XLConnect"))
lst <- readWorksheet(wb, sheet = getSheets(wb))

lst is a named list whose names correspond to the sheet names. lst是一个命名列表,其名称与图纸名称相对应。 Note that readWorksheet is vectorized and therefore you can read multiple worksheets with a single readWorksheet call. 请注意, readWorksheet是矢量化的,因此您可以通过一个readWorksheet调用读取多个工作表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM