Export R data to CSV

How to export R data frames to CSV or TSV with base R, readr and data.table, what the conversion keeps and loses, and how to keep the file usable once it is shared.

Exporting R data to CSV turns an in-memory data frame into delimited plain text that almost any program can open. That portability is why CSV is the default choice. The same conversion also drops most of what R knows about your data, so check whether the result still fits how it will be used. This review covers the main ways to write CSV and TSV files from R, what the conversion changes, and when another format serves better.

Is CSV the right export for your purpose?

CSV fits when people or programs outside R need to read the data. Typical cases are sending a table to a colleague, loading it into a database or spreadsheet, sharing it with Python or another language, keeping it in version control, or archiving it in a form that will still open years from now. Plain delimited text is easy to read, search and compare line by line, and it does not depend on any one piece of software.

The main tradeoff is that CSV stores only text. Column types, factor level order, time zones, variable labels and other attributes are not kept, and whoever reads the file has to guess or restate them. If the data is going back into R, or the exact structure matters, a typed format is the safer choice. Those alternatives are covered near the end.

The main ways to write a CSV from R

Three functions cover most needs. Base R's write.csv needs no extra packages. readr's write_csv, part of the tidyverse, has more predictable defaults. data.table's fwrite is built for speed on large tables.

# Base R
write.csv(df, "results.csv", row.names = FALSE)

# readr
readr::write_csv(df, "results.csv")

# data.table
data.table::fwrite(df, "results.csv")
Function | Row names | Missing values written as | Quoting | Encoding
write.csv | Written by default; set row.names = FALSE to omit | NA | Character and factor columns quoted by default | Set with fileEncoding
readr::write_csv | Not written | NA by default; change with na = | Only when needed | Always UTF-8; write_excel_csv adds a byte order mark
data.table::fwrite | Not written by default | Empty field by default | Only when needed by default | UTF-8 text; bom = TRUE adds a byte order mark

The row names default in write.csv causes a common surprise. By default it writes an unnamed first column of row names, which shows up as an extra column (often named X) when the file is read back. Unless the row names carry real information, pass row.names = FALSE.

Writing TSV or other delimiters

Tab-separated files work the same way with a different separator. They suit data where commas appear inside values, such as addresses or free-text notes.

write.table(df, "results.tsv", sep = "\t", row.names = FALSE)
readr::write_tsv(df, "results.tsv")
data.table::fwrite(df, "results.tsv", sep = "\t")

Turning quoting off (quote = FALSE) makes the output look cleaner. If any value contains the delimiter, a quote character or a line break, though, the file will no longer parse correctly. For locales that use a comma as the decimal mark, base R provides write.csv2, which uses a semicolon as the separator and a comma for decimals.

What the export actually changes

CSV export is serialization, not a change to how characters look. Each value is converted to a text representation and written between delimiters. The text is not reversed, mirrored or restyled, and the data frame in R is left untouched. Keep two things apart: what is stored in the file, and what a program shows when it opens the file. A spreadsheet may display a column as dates or rounded numbers while the file holds different text. Opening the file in a plain text editor shows what was actually written.

  • Types: numbers, logicals and dates become plain text, and the reading program has to infer or be told the types again.
  • Factors: written as their labels; level order and unused levels are lost.
  • Dates and times: readr and fwrite write ISO 8601 by default and convert date-times to UTC; base R writes whatever the object's format method produces. Check the defaults for your installed version.
  • Missing values: written as NA or as an empty field depending on the function, and the reader must interpret them the same way.
  • Attributes and labels: variable labels, such as those imported with the haven package, and other attributes are not written.
  • List columns: nested values do not map to a single cell and usually need to be flattened or converted to character first.
  • Numbers: formatting can affect precision or notation; a round-trip check (below) shows whether values survive.

Character content is copied as stored. In UTF-8, accented letters, combining marks and multi-code-point emoji are written as their original code point sequences and are not split or reordered. Whether they display correctly afterward depends on the reading program assuming the same encoding.

Encoding and special characters

UTF-8 is the safest encoding for sharing. readr always writes UTF-8. Since R 4.2, base R on Windows also uses UTF-8 as its native encoding. Older setups may write in a legacy code page unless you set fileEncoding = "UTF-8" explicitly.

Some spreadsheet applications assume a legacy encoding when opening a CSV unless the file starts with a byte order mark. readr::write_excel_csv and fwrite with bom = TRUE add one. Other tools may show the mark as stray characters at the start of the first header. Add it only when the destination needs it.

Will the file stay usable after sharing?

Most problems happen at the receiving end. Spreadsheet programs, including Excel, are known to reinterpret CSV values when opening a file directly. They may drop leading zeros from codes such as US ZIP codes, show long ID numbers in scientific notation and lose digits, convert date-like strings into dates, and treat cells that begin with =, +, - or @ as formulas. The last behavior is a security concern known as CSV or formula injection. How this plays out depends on the application, its version, its settings and how the file is opened. No specific version was tested for this review.

Storing a column as character in R does not prevent these conversions, because a CSV carries no type information. The more reliable fix is on the receiving side. Using the spreadsheet's text import feature and setting column types there avoids most of these conversions. For values that must arrive exactly as written, consider sending a native workbook or another typed format instead.

Separator and locale expectations can also clash. A comma-separated file opened in a region that expects semicolons may land entirely in one column. Line endings can differ between platforms and functions; most modern tools accept either style, but strict parsers may not.

For accessibility, CSV has an advantage: it is plain text with no visual-only formatting. How well a screen reader handles it depends on the program used to open it, and no assistive technology was tested here. In general, a single header row with clear, descriptive column names, one value per cell, and no notes mixed into data cells makes the table easier to navigate and to search, filter and edit in any tool.

Check the export before you share it

A quick round trip shows what the file keeps. Write the file, read it back, and compare it with the original.

write.csv(df, "results.csv", row.names = FALSE, fileEncoding = "UTF-8")
check <- read.csv("results.csv", fileEncoding = "UTF-8")
all.equal(df, check)

Some differences are expected, such as factors coming back as character or dates coming back as text. Any other difference, such as changed numbers, lost characters or shifted columns, points to a quoting, encoding or precision problem worth fixing before the file goes out.

  • Open the file in a plain text editor to confirm the header, delimiter, quoting and missing-value markers.
  • Confirm the encoding is UTF-8, and add a byte order mark only if the destination needs it.
  • Check columns with leading zeros, long IDs and date-like strings in the destination program.
  • Look for text values that start with =, +, - or @ if the file will be opened in a spreadsheet.

When another format is the better choice

  • Reloading in R: saveRDS and readRDS keep the exact object, including types, factor levels and attributes.
  • Typed exchange with other languages: Parquet or Feather files, written with the arrow package, keep column types.
  • Spreadsheets with formatting or multiple sheets: packages such as writexl or openxlsx write native workbooks.
  • Large files: fwrite is designed for speed, and both readr and fwrite can write compressed output, such as files ending in .gz.

Gaps in this review

This overview relies on documented function behavior and widely reported spreadsheet behavior, not on new tests. It includes no speed benchmarks, no checks against specific package or application versions, and no assistive technology testing. Defaults can change between releases, so check the help page for your installed version, for example ?write.csv or ?readr::write_csv, before relying on a particular default.