01 / 21
.xlsx

Reading Excel Files
in R with readxl

Learn how to import .xlsx and .xls spreadsheets into R using the readxl package: from installation to data frames.

R Programming

Why Read Excel Files in R?

Excel is one of the most widely used formats for storing and sharing data in business, science, and education. To analyze that data in R, you need a reliable way to import spreadsheet files directly into your R environment.

What is the readxl Package?

readxl
A lightweight R package designed specifically to read Excel files (.xls and .xlsx) into R data frames.
No Java Required
Unlike some alternatives, readxl has no external dependencies: no Java, no Perl, no extra software.
Part of tidyverse
readxl is included in the tidyverse ecosystem, making it a trusted, well-maintained choice.

Key Vocabulary

1
Package: A bundle of reusable R functions you can install and load.
2
Data frame: R's main structure for tabular data: rows and columns, like a spreadsheet in memory.
3
Worksheet (sheet): A single tab inside an Excel workbook. One .xlsx file can contain many sheets.
4
File path: The address that tells R where to find your file on disk, e.g., "data/grades.xlsx".

The Core Function: read_excel()

# Install once (if needed)
install.packages("readxl")

# Load the package
library(readxl)

# Read an Excel file
my_data <- read_excel("students.xlsx")

How Data Flows into R

.xlsx file on your disk read_excel() readxl function Data Frame in R memory

read_excel() vs read.csv()

read_excel() read.csv()
File format .xlsx, .xls .csv
Package needed readxl Built-in (base R)
Multiple sheets ✓ Yes ✕ No
Cell formatting Preserves types (date, etc.) Infers basic types (numeric, text)
Returns Tibble Data frame

Important Arguments

path
The file location: "data/scores.xlsx"
sheet
Which worksheet to read: by name or number
skip
Number of rows to skip before reading data
col_names
TRUE/FALSE: does the first row contain headers?

How to Apply It

1
Install readxl: Run install.packages("readxl") once in your R console.
2
Load the library: At the top of your script, write library(readxl).
3
Identify the file path: Find where the .xlsx file is stored relative to your working directory.
4
Call read_excel(): Assign the result to a variable: df <- read_excel("file.xlsx")
5
Explore the data: Use head(df), str(df), or View(df) to inspect your data frame.

Example 1: Basic Import

library(readxl)

# Read the first sheet of grades.xlsx
grades <- read_excel("grades.xlsx")

# View the first 6 rows
head(grades)

Example 2: Selecting a Sheet

# List all sheet names
excel_sheets("survey.xlsx")
# [1] "Responses" "Demographics" "Summary"

# Read the second sheet by name
demo <- read_excel("survey.xlsx", sheet = "Demographics")

# Or read by position
demo <- read_excel("survey.xlsx", sheet = 2)

Example 3: Advanced Options

# Skip 2 header rows, read specific range
sales <- read_excel(
  "report.xlsx",
  sheet = "Q4",
  skip = 2,
  col_names = TRUE,
  range = "A1:D50"
)

What You Get Back

name grade score "Alice" "A" 95 "Bob" "B+" 88 "Carla" "A-" 91 1 2 3 <chr> <chr> <dbl> Column types are auto-detected

Useful Helper Functions

excel_sheets()
Lists all sheet names in a workbook as a character vector.
read_xlsx()
Forces reading as .xlsx format. Identical to read_excel() for .xlsx files.
read_xls()
Forces reading as .xls (older format). Use when auto-detection fails.

Common Mistakes

✕ Forgetting library()
You installed readxl but forgot to call library(readxl) at the top of your script. R will say "could not find function".
✕ Wrong file path
The file is in a different folder. Use getwd() to check your working directory and adjust the path.
✕ Using read.csv()
Trying to read an .xlsx file with read.csv() produces garbled data or errors. Always match the function to the file type.

Summary Reference

Task Code
Install package install.packages("readxl")
Load package library(readxl)
Read first sheet read_excel("file.xlsx")
Read specific sheet read_excel("file.xlsx", sheet = "Name")
List sheets excel_sheets("file.xlsx")
Skip rows read_excel("file.xlsx", skip = 3)
Read cell range read_excel("file.xlsx", range = "A1:C10")

Question 1 of 5

Which R function is used to read an Excel file into a data frame using the readxl package?

Question 2 of 5

Before using read_excel() in your script, what must you do every time you start a new R session?

Question 3 of 5

An Excel workbook has three sheets: "Sales", "Returns", and "Summary". Which code reads the "Returns" sheet?

Question 4 of 5

Which function from readxl returns a list of all sheet names in a workbook?

Question 5 of 5

Your spreadsheet has 3 rows of title text above the actual data. Which argument tells read_excel() to skip those rows?