Read and write CSV files in R
How base R, readr and data.table read and write CSV files, what changes during import and export, and when CSV is or isn't the right format.
Reading a CSV file into R, or writing a data frame back out, looks like a simple conversion between a table and plain text. It is also a transformation. R has to decide what each field means, and the exported file has to be read correctly by whatever opens it next. This review covers what changes along the way, where meaning can be lost, and when CSV is the right choice.
Is CSV a suitable format for your R workflow?
CSV is a good choice when you need to exchange tabular data with other people, spreadsheets, databases or other languages. Its main benefit is that it is plain text. Almost any tool can open it, people can read it directly, and it works well with version control and quick inspection.
The main tradeoff is that CSV stores characters, not data types. It carries no record of which column was a date, a factor with ordered levels, an integer ID with leading zeros, or a value in a particular encoding. Every reader has to guess or be told. If the data only needs to move between R sessions, R's own serialized format (saveRDS and readRDS) keeps types and attributes intact and avoids that guesswork. CSV is the better option when the file must be shared, inspected by hand or opened outside R. It is a poor option when exact types must survive and you cannot control how the receiving side reads the file.
The main tools for reading and writing CSV in R
| Approach | Read function | Write function | Notable defaults |
| Base R (utils) | read.csv, read.csv2 | write.csv, write.csv2 | Writes row names unless row.names = FALSE; quotes character fields by default; since R 4.0.0, strings are not converted to factors by default |
| readr (tidyverse) | read_csv, read_csv2 | write_csv, write_excel_csv | Guesses column types from a sample of rows and reports the result; returns a tibble; writes UTF-8 and quotes only when needed |
| data.table | fread | fwrite | Designed for large files; detects the separator automatically; returns a data.table |
The "2" variants (read.csv2, write.csv2, read_csv2) are for the semicolon-separated, decimal-comma convention common in many European locales. Using the wrong variant is a frequent cause of every row ending up in a single column, or of numbers being read as text.
No benchmark evidence was supplied for this review, so no speed comparison is made here. data.table's documentation describes fread and fwrite as built for performance on large files. Whether that matters for your data depends on file size and should be checked on your own files.
What reading and writing actually change
A CSV file is a sequence of characters. Commas, or another delimiter, separate fields, and line breaks separate records. Quotation marks let a field contain the delimiter or a line break. Nothing in the file says what a field means. When R reads the file, it parses those characters into typed columns. When it writes, it converts typed values back into characters. Both steps can alter the stored data, which is different from how a spreadsheet merely displays it.
Take this small input file:
id,zip,joined
1,02134,2024-01-15
2,10001,2024-02-03
Base R and readr interpret it differently:
# Base R
df <- read.csv("members.csv")
str(df)
# id: int, zip: int (02134 becomes 2134), joined: chr
# readr
library(readr)
tb <- read_csv("members.csv")
# id: dbl, zip: dbl (02134 becomes 2134), joined: date
Both readers drop the leading zero from the ZIP code because the field looks numeric. The value is changed on import, not just shown differently, and writing it back out produces 2134. Base R leaves the date as text, while readr parses it as a Date. To keep identifiers exactly as written, declare the column type:
read.csv("members.csv", colClasses = c(zip = "character"))
read_csv("members.csv", col_types = cols(zip = col_character()))
data.table::fread("members.csv", colClasses = list(character = "zip"))
Writing introduces its own changes:
- Row names: write.csv adds an extra first column of row names with an empty header unless you pass row.names = FALSE. Reading that file back with read.csv yields an extra column, typically named X.
- Missing values: both write.csv and write_csv write NA as the literal text NA by default. Other tools may read that as a string rather than a missing value. Use the na argument to write an empty field instead if the destination expects one.
- Factors: a factor is written as its labels. Level order and unused levels are lost, so re-reading gives plain character data unless you rebuild the factor.
- Dates and times: these are written as text. Time zone information may not survive in the form the receiving tool expects, so check date-time columns specifically.
- Quoting: base R quotes all character fields by default, while readr quotes only fields that need it. Both produce valid CSV. Line-based diffs between the two will still show many changes even when the data is identical.
Encoding: the change you often cannot see
Text encoding decides how characters outside basic ASCII, such as accented letters, non-Latin scripts and emoji, are stored as bytes. readr reads and writes UTF-8 by default. Base R's read.csv and write.csv rely on the session's native encoding unless you set fileEncoding. On systems where the native encoding is not UTF-8, that can produce garbled characters.
# Be explicit when non-ASCII text is involved
df <- read.csv("names.csv", fileEncoding = "UTF-8")
write.csv(df, "names_out.csv", row.names = FALSE, fileEncoding = "UTF-8")
# readr: declare a non-UTF-8 source encoding on read
tb <- read_csv("legacy.csv", locale = locale(encoding = "latin1"))
Reading or writing a CSV does not reverse, mirror or restyle characters. The values stay in their stored order. An encoding mismatch, however, can permanently replace characters with the wrong ones if the garbled result is saved again. The fix is to correct the encoding at read time, not to repair the output afterward.
Will the file stay usable after sharing?
Because CSV is plain text, copying and pasting it does not change its content in principle. Usability depends on how the next application interprets it. Points worth checking:
- Spreadsheets: spreadsheet applications apply their own type guessing when opening a CSV. They may strip leading zeros, reformat dates or convert long numeric IDs, even if R wrote the values correctly. Some versions of Excel also misread UTF-8 files that lack a byte-order mark. readr's write_excel_csv adds that mark for this reason. This review has no tested evidence about specific Excel versions, so verify with the version your recipients use.
- Locale: recipients whose software expects semicolon separators and decimal commas may need write.csv2 or write_csv2 output, or explicit import settings on their side.
- Embedded delimiters and line breaks: fields containing commas, quotes or newlines must be quoted. All three R packages handle this when writing, but hand-edited files or simple text tools may break the structure.
- Round trips: export and re-import a sample, then compare it with the original before relying on the file. Column types, missing values, row counts and any non-ASCII text are the usual places where differences show up.
On accessibility: a CSV file has no visual formatting, so it adds no styled characters or reading-order changes that could confuse assistive technology. How a screen reader presents the table depends on the application used to open it, such as a spreadsheet, a text editor or a data viewer. No documentation or test results for specific assistive technologies were supplied for this review. If accessibility matters for your recipients, test with the tools they actually use. For wide or complex tables, a short description of the columns sent alongside the file can help.
A practical baseline
library(readr)
# Read with explicit types for anything that must not be guessed
data <- read_csv(
"input.csv",
col_types = cols(id = col_character(), joined = col_date()),
na = c("", "NA")
)
# Write for general sharing
write_csv(data, "output.csv", na = "")
# Write for recipients opening the file in Excel
write_excel_csv(data, "output_excel.csv", na = "")
# Base R equivalent without row names
write.csv(data, "output_base.csv", row.names = FALSE, na = "", fileEncoding = "UTF-8")
Bottom line
Reading and writing CSV in R is dependable when you control type guessing and encoding. Declare column types for identifiers and dates, turn off row names when writing, choose how missing values appear, and state the encoding explicitly whenever the text is not plain ASCII. Use CSV for sharing and inspection. For data that only moves between R sessions and must keep its exact types, use RDS instead.