Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data.table::fread Read all worksheets in an Excel workbook

Tags:

My Excel document my.xlsx has two Sheets named Sheet1 and Sheet2. I want to read all worksheets in an Excel workbook using fread function from data.table R package. The following code just read the active worksheet. Wonder how to read all worksheets without knowing their names. Thanks

df3 <- data.table::fread("in2csv my.xlsx")
> names(df3)
[1] "A" "B"
> df3
   A  B
1: 1  2
2: 2  4
3: 3  6
4: 4  8
5: 5 10
like image 909
MYaseen208 Avatar asked Jun 11 '19 14:06

MYaseen208


People also ask

How do I read data from multiple sheets in Excel?

On the Data tab, under Tools, click Consolidate. In the Function box, click the function that you want Excel to use to consolidate the data. In each source sheet, select your data, and then click Add. The file path is entered in All references.


1 Answers

I used openxlsx::read.xlsx the last time I needed to read many sheets from an 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)
}

Note: This assumes each worksheet has identical column structure and data types. You may need to standardize\normalize the data (ex. tmp_sheet <- as.data.frame(sapply(tmp_sheet,as.character), stringsAsFactors=FALSE)), or load each sheet into it's own dataframe and pre-process further before merging.

like image 104
M.Viking Avatar answered Sep 20 '22 17:09

M.Viking