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.
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?
Key Vocabulary
The Core Function: read_excel()
install.packages("readxl")
# Load the package
library(readxl)
# Read an Excel file
my_data <- read_excel("students.xlsx")
How Data Flows into R
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
How to Apply It
Example 1: Basic Import
# Read the first sheet of grades.xlsx
grades <- read_excel("grades.xlsx")
# View the first 6 rows
head(grades)
Example 2: Selecting a Sheet
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
sales <- read_excel(
"report.xlsx",
sheet = "Q4",
skip = 2,
col_names = TRUE,
range = "A1:D50"
)
What You Get Back
Useful Helper Functions
Common Mistakes
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?