Read Excel files in R

Today we’ll be learning how to read excel files and write output files in R. There are multiple packages in R. We’ll see few packages available and how to use it.

Few Packages available– xlsx, openxlsx, readxl, xlsReadWrite, gdata, XLConnect

I’ve taken input File ‘Test.xlsx’ with two sheets named ‘Sheet1’ and ‘Sheet2’

xlsx and openxlsx package

Commands or functions for xlsx and openxlsx package in R is same(can be seen below)

#installing and loading the packages
install.packages("xlsx")
install.packages("openxlsx")

library(xlsx)
library(openxlsx)

#read files
abc<- read.xlsx("Test.xlsx",sheet = "Sheet1")
abc1<- read.xlsx("Test.xlsx",sheet = "Sheet2")

#writing single sheet to excel
write.xlsx(abc, "abc.xlsx")

#writing multiple sheets to excel
list_of_datasets <- list( "SheetName1" = abc, "SheetName2" = abc1)
write.xlsx(list_of_data, file = "Output.xlsx")

readXL and writexl package

readXL package in R helps to read excel files in R and writexl package helps to export the output as an Excel file. Individually these packages can be used to read and write.

#install and load packages to read Excel files
install.packages("readXL")
library(readXL)

#read files
abc<- read_xlsx("Test.xlsx",sheet = "Sheet1")
abc1<- read_xlsx("Test.xlsx",sheet = "Sheet2")

#install and load packages to export as Excel files
install.packages("writexl")
library(writexl)

#writing single file output
write_xlsx(abc, "aa.xlsx")

#writing multiple sheets to excel
list_of_datasets <- list( "SheetName1" = abc, "SheetName2" = abc1)
write_xlsx(list_of_datasets, file = "Output.xlsx")

These 3 packages widely used to read Excel files in R and exporting as Excel file in R.

Further Reading-
xlsx
openxlsx
readxl
gdata
XLConnect

Keep visiting Analytics Tuts for more tutorials.

Thanks for reading! Comment your suggestions and queries.

Leave a Reply

Your email address will not be published. Required fields are marked *