R provides several packages like readxl
, xlsx
, and openxlsx
to read or import excel files into R DataFrame. These packages provide several methods with different arguments which help us read excel files effectively.
We have also provided quick articles for reading CSV files and writing CSV files using R base functions as well as using readr
package, which is 10 times faster than R base functions.
In this article, you will learn how to read data from an excel file with extension xls or xlsx into R DataFrame. This can be done by using readxl::read_excel()
and xlsx:read.xlsx()
1. Quick Examples
Following are quick examples of how to read or import excel files into R DataFrame.
# Load readxl package
library("readxl")
# Read xlsx files
df = read_excel("/Users/admin/new_file.xlsx")
df = read_xlsx("/Users/admin/new_file.xlsx")
df = read_xsl("/Users/admin/new_file.xsl")
# Read sheet names 'sheet_name' from excel file
df = read_excel("/Users/admin/new_file.xlsx", sheet='sheet_name')
# Read sheet 3 from excel file
df = read_excel("/Users/admin/new_file.xlsx", sheet=3)
#Load xlsx package
library("xlsx")
#Read first sheet from new_file.xlsx file
df = read.xlsx(file='/Users/admin/new_file.xlsx', sheetIndex=1)
# Load openxlsx
library(openxlsx)
# Read excel file
read.xlsx('/Users/admin/new_file.xlsx')
2. Import Excel files into R using readxl package
Use read_excel()
function from readxl
package to read or import an excel file (xlsx or xls) as R DataFrame. In order to use readxl
 library, you need to first install it by using install.packages('readxl')
. Once installation completes, load the readxl
 library to use this read_excel()
 method. To load a library in R use library("readxl")
.
read_excel()
 calls excel_format()
 to check if path
 is xls or xlsx. If you know the file extension, use read_xls()
 and read_xlsx()
 directly.
2.1 Install readxl Package
In order to use readxl
package in R to work with excel files, first, you need to install it using install.packages()
. Run the below command in R or in RStudio.
# Install readxl package
install.packages("readxl")
2.2 Syntax of read_excel() Function
Following is the syntax of read_excel()
function.
# Syntax of read_excel()
read_excel(path,sheet = NULL,range = NULL,col_names = TRUE,
col_types = NULL, na = "", trim_ws = TRUE, skip = 0,
n_max = Inf, guess_max = min(1000, n_max), progress = readxl_progress(),
.name_repair = "unique"
)
2.3 Import Excel File using readxl Package
Let’s use the above syntax of read_excel()
to import an excel file into an R DataFrame. If you wanted to read the data from a specific sheet use the sheet
argument.
# Load readxl package
library("readxl")
# Read xlsx files
df = read_excel("/Users/admin/new_file.xlsx")
(or)
df = read_xlsx("/Users/admin/new_file.xlsx")
If you have xls file then use the below methods to read or import it as DataFrame.
# Read xsl files
df = read_excel("/Users/admin/new_file.xsl")
(or)
df = read_xsl("/Users/admin/new_file.xsl")
2.4 Read Data from Specific Excel Sheet
If you have multiple sheets in an excel file (xlsx, xsl) use the sheet
argument to read or import the data from a specific sheet. Pass either sheet name or sheet index to this argument.
# Read sheet names 'sheet_name' from excel file
df = read_excel("/Users/admin/new_file.xlsx", sheet='sheet_name')
#or
# Read sheet 3 from excel file
df = read_excel("/Users/admin/new_file.xlsx", sheet=3)
3. Import Excel files using xlsx package
Package xlsx
is another package R to read or import excel files into DataFrame. It is a java-based solution, for reading, writing and formatting excel files in R.
Use read.xlsx()
function from xlsx
package to read or import an excel file (xlsx or xls) as R DataFrame. In order to use xlsx
 library, you need to first install it by using install.packages('xlsx')
. Once installation completes, load the xlsx
 library to use this read_xlsx()
 method. To load a library in R use library("xlsx")
.
3.1 Install xlsx Package
Run the below command in R or RStudio to install xlsx
package.
# Install readxl package
install.packages("xlsx")
3.2 Syntax
The xslx
package provides 2 function read.xlsx()
and read.xlsx2()
, below are their syntaxes. Use read.xlsx2() if you have big files as it performs better than read.xlsx().
# Syntax of read.xlsx() & read.xlsx2()
read.xlsx(file, sheetIndex, header=TRUE)
read.xlsx2(file, sheetIndex, header=TRUE)
3.3 Using read.xlsx() Example
Let’s use the above syntax of read.xslx() to read an excel file into an R DataFrame. If you wanted to read the data from a specific sheet use the sheetIndex
argument.
#Load xlsx package
library("xlsx")
#Read first sheet from new_file.xlsx file
df = read.xlsx(file='/Users/admin/new_file.xlsx', sheetIndex=1)
4. Import Excel files using openxlsx Package
The openxlsx
is another package that can be used to perform operations with excel files. Use read.xlsx()
function from this package to read the worksheet.
#Install openxlsx package
install.packages("openxlsx")
# Load openxlsx
library(openxlsx)
# Read excel file
read.xlsx('/Users/admin/new_file.xlsx')
5. Conclusion
In this article, you have learned how to read or import an excel file into R DataFrame by using methods from package openxlsx
, xlsx
, and readxl
packages.
Related Articles
- How to Import Text File as a String in R
- How to Read Text File to DataFrame in R
- How to Read CSV From URL in R
- How to Read Multiple CSV Files in R
- How to Read CSV Files in R
- How to Export CSV in R Using write.csv()
- How to Export Excel files in R
- Export to Excel in R (XLSX or XLS)