R Excel files
last modified November 19, 2026
Excel workbooks are still the most common way to pass a table between people,
and R can read and write them without opening Excel. The readxl
package reads .xlsx and .xls files into tibbles, and
the writexl package writes data frames to .xlsx
files. Neither package needs Java or a running Excel installation, which makes
them reliable on a server. This article covers single sheets, multiple sheets,
column types, ranges, missing values, dates, and a complete summary workbook.
The examples use readxl 1.6.0, writexl 2.0.1, and R 4.5.2.
The examples need the readxl and writexl packages, which are not part of base
R. Install them once with
install.packages(c("readxl", "writexl")); the
installing packages section of the
introduction explains the details.
Writing and reading a workbook
write_xlsx takes a data frame and a file name and writes a
workbook with one sheet. read_excel takes a file name and returns
a tibble, the tidyverse table type that prints its column types under the
header. excel_sheets lists the sheets in a file, which is useful
when the workbook was produced by someone else. The default sheet is called
Sheet1.
library(writexl)
library(readxl)
scores <- data.frame(
name = c("Ada", "Grace", "Alan"),
score = c(91, 88, 84)
)
write_xlsx(scores, "scores.xlsx")
cat("sheets:", excel_sheets("scores.xlsx"), "\n")
back <- read_excel("scores.xlsx")
print(back)
cat("class:", class(back)[1], "\n")
unlink("scores.xlsx")
The file contains a header row with the two variable names and three data
rows. excel_sheets returns Sheet1, the name that
write_xlsx gave to the only sheet. read_excel returns
a tibble with a character column and a numeric column, and it prints the types
as <chr> and <dbl> without being asked.
The script deletes the file at the end, so a repeated run starts from a clean
directory.
$ Rscript write_read.R sheets: Sheet1 # A tibble: 3 × 2 name score <chr> <dbl> 1 Ada 91 2 Grace 88 3 Alan 84 class: tbl_df
Working with sheets
A workbook can hold several sheets, and write_xlsx accepts a named
list of data frames, one per sheet. The names of the list become the sheet
names. read_excel reads the first sheet by default and takes a
sheet argument with a name or a position. Reading one sheet at a
time keeps the code explicit about which table it uses.
library(writexl)
library(readxl)
january <- data.frame(day = 1:3, temp = c(2.5, 3.0, 1.5))
february <- data.frame(day = 1:3, temp = c(4.0, 5.5, 6.0))
write_xlsx(list(January = january, February = february), "weather.xlsx")
cat("sheets:", excel_sheets("weather.xlsx"), "\n")
second <- read_excel("weather.xlsx", sheet = "February")
print(second)
cat("mean:", mean(second$temp), "\n")
unlink("weather.xlsx")
The list names January and February become the sheet
names, and excel_sheets reports them in that order. The read
selects the second sheet by name, so adding another sheet would not change the
result. The mean of the February temperatures is 5.166667, the average of 4,
5.5, and 6. A sheet name is safer than a position when the workbook is edited
by other people.
$ Rscript sheets.R
sheets: January February
# A tibble: 3 × 2
day temp
<dbl> <dbl>
1 1 4
2 2 5.5
3 3 6
mean: 5.166667
Column types and missing values
read_excel guesses each column type from the first rows of the
sheet. The col_types argument overrides the guess. It accepts one
type for all columns, such as "text", or a vector with one type per
column, such as c("text", "numeric"). The types are
"skip", "guess", "logical",
"numeric", "date", "text", and
"list". An empty cell becomes NA.
library(writexl)
library(readxl)
orders <- data.frame(
id = c("A-1", "A-2", "A-3"),
amount = c(10.5, NA, 7.25)
)
write_xlsx(orders, "orders.xlsx")
auto <- read_excel("orders.xlsx")
print(auto)
cat("amount class:", class(auto$amount)[1], "\n")
as_text <- read_excel("orders.xlsx", col_types = "text")
print(as_text)
cat("amount as text:", as_text$amount[1], "\n")
cat("missing amounts:", sum(is.na(auto$amount)), "\n")
unlink("orders.xlsx")
The automatic read keeps id as text because the values contain a
letter, and reads amount as numeric. The blank cell in the second
row becomes NA, so sum(is.na(...)) returns one. The
second read forces every column to text, so the amount is the string
"10.5" and the missing value prints as <NA>. The
na argument of read_excel can add strings that should
be treated as missing, for example na = c("", "NA", "n/a").
$ Rscript types.R # A tibble: 3 × 2 id amount <chr> <dbl> 1 A-1 10.5 2 A-2 NA 3 A-3 7.25 amount class: numeric # A tibble: 3 × 2 id amount <chr> <chr> 1 A-1 10.5 2 A-2 <NA> 3 A-3 7.25 amount as text: 10.5 missing amounts: 1
Reading a range
A sheet often contains more than one table, or a title above the header.
read_excel accepts an Excel range such as "A2:B3",
which selects a rectangle of cells, and col_names supplies the
names when the selected range does not include a header row. The argument
n_max limits the number of data rows, and skip
skips rows above the header. These options avoid reading a whole workbook
just to extract one block.
library(writexl)
library(readxl)
report <- data.frame(
city = c("Bratislava", "Kosice", "Zilina", "Nitra"),
temp = c(12.5, 10.0, 8.5, 11.0)
)
write_xlsx(report, "report.xlsx")
selected <- read_excel("report.xlsx", range = "A2:B3",
col_names = c("city", "temperature"))
print(selected)
first_two <- read_excel("report.xlsx", n_max = 2)
print(first_two)
unlink("report.xlsx")
The range starts at row 2, so the header written by write_xlsx is
not included, and col_names provides the two names. The result has
the two cities in rows 2 and 3 of the sheet, Bratislava and Kosice.
n_max = 2 reads the header and the first two data rows, which is
the same as asking for the first two records rather than a cell rectangle. The
two calls return tibbles with the selected columns and rows.
$ Rscript cell-range.R # A tibble: 2 × 2 city temperature <chr> <dbl> 1 Bratislava 12.5 2 Kosice 10 # A tibble: 2 × 2 city temp <chr> <dbl> 1 Bratislava 12.5 2 Kosice 10
Dates
Excel stores a date as a serial number with a display format, and a date-time
as a serial number with a fractional part. writexl recognises R
Date and POSIXct columns and writes the matching cell
types. readxl returns a date cell as POSIXct in UTC,
not as Date, even when col_types = "date" is given.
The class is the same for a date and a date-time, so a date-only column needs
an explicit conversion.
library(writexl)
library(readxl)
visits <- data.frame(
date = as.Date(c("2026-01-05", "2026-02-11", "2026-03-20")),
visitors = c(120, 135, 150)
)
write_xlsx(visits, "visits.xlsx")
auto <- read_excel("visits.xlsx")
print(auto)
cat("auto class:", class(auto$date)[1], "\n")
cat("as date:", format(as.Date(auto$date[1])), "\n")
typed <- read_excel("visits.xlsx", col_types = c("date", "numeric"))
print(typed)
cat("typed class:", class(typed$date)[1], "\n")
unlink("visits.xlsx")
The tibble prints the date column with a <dttm> type and a
time of 00:00:00, because the value is a POSIXct
midnight in UTC. as.Date drops the time and returns the calendar
date, which prints as 2026-01-05. Specifying
col_types = "date" does not change the class in readxl 1.6.0; it
only tells the reader that the column is a date. Working with Date
when the time of day has no meaning avoids time-zone surprises in later
calculations.
$ Rscript dates.R # A tibble: 3 × 2 date visitors <dttm> <dbl> 1 2026-01-05 00:00:00 120 2 2026-02-11 00:00:00 135 3 2026-03-20 00:00:00 150 auto class: POSIXct as date: 2026-01-05 # A tibble: 3 × 2 date visitors <dttm> <dbl> 1 2026-01-05 00:00:00 120 2 2026-02-11 00:00:00 135 3 2026-03-20 00:00:00 150 typed class: POSIXct
A complete example
The final script reads a workbook with one sheet per quarter, adds the sheet
name as a column, and combines the parts into one table. It then sums the
amounts by representative with aggregate, writes the detail and
the totals to a new workbook, and lists the sheets in the result. This is the
usual shape of an Excel report: several input sheets, one combined table, and
a summary sheet for the reader.
library(writexl)
library(readxl)
q1 <- data.frame(rep = c("Ada", "Bob", "Ada", "Bob"),
amount = c(120, 90, 150, 75))
q2 <- data.frame(rep = c("Ada", "Bob", "Ada"),
amount = c(200, 110, 50))
write_xlsx(list(Q1 = q1, Q2 = q2), "sales.xlsx")
combined <- do.call(rbind, lapply(excel_sheets("sales.xlsx"), function(sheet) {
part <- read_excel("sales.xlsx", sheet = sheet)
part$quarter <- sheet
part
}))
totals <- aggregate(amount ~ rep, data = combined, FUN = sum)
write_xlsx(list(Detail = combined, Totals = totals), "summary.xlsx")
print(combined)
print(totals)
cat("sheets:", excel_sheets("summary.xlsx"), "\n")
unlink(c("sales.xlsx", "summary.xlsx"))
The combined table has seven rows, because Q1 has four records and Q2 has
three. aggregate groups by rep and returns one row per
representative: Ada totals 520 and Bob totals 275. write_xlsx
writes the detail and the totals as two sheets of summary.xlsx,
and excel_sheets confirms the names. For a larger workbook, the
same result can be written with dplyr verbs or with
pivot_longer from tidyr before the aggregation.
$ Rscript workbook-report.R # A tibble: 7 × 3 rep amount quarter <chr> <dbl> <chr> 1 Ada 120 Q1 2 Bob 90 Q1 3 Ada 150 Q1 4 Bob 75 Q1 5 Ada 200 Q2 6 Bob 110 Q2 7 Ada 50 Q2 rep amount 1 Ada 520 2 Bob 275 sheets: Detail Totals
Next steps
The data I/O article covers CSV, RDS, and plain
text files, which are often a better fit than a workbook for data that a
script will read again. The tibbles article
explains the table type that read_excel returns, and the
data frames article covers the base
equivalent. The dplyr and
tidyr articles show the pipeline alternative to
aggregate for larger tables.
Source
In this article we have written a workbook with write_xlsx, read
it back with read_excel, and listed its sheets with
excel_sheets. Multiple sheets came from a named list, column
types and missing values were controlled through col_types, and a
range selected a block of cells without reading the whole sheet. Dates were
written as date cells and read back as UTC POSIXct values, and the
final script combined quarterly sheets into a detail table and a totals sheet.
Author
List all R Language tutorials.