Export CSV from R: Choosing the Right Function and Avoiding Common Pitfalls

A practical review of how to export a CSV file in R with write.csv, readr and data.table: what each changes in your data, and how to keep the output readable in other tools.

Exporting data to CSV from R takes one line of code, but the result is not always the same file. The function you choose decides how row names, missing values, quoting, dates, decimal marks and character encoding are written. Those choices determine whether the file opens cleanly in a spreadsheet, loads correctly into another program, or quietly loses information along the way. This review covers the main options, what each one changes when it turns a data frame into delimited text, and when CSV is the wrong format.

Short recommendation

For most work, export with readr::write_csv() or data.table::fwrite(). Both leave out row names by default and write consistent, predictable output. Use base R's write.csv() when you cannot add packages, but set row.names = FALSE unless you need the row names as a column. If the file is meant to be opened in Excel and contains non-ASCII characters, readr::write_excel_csv() is the most direct choice because it adds a byte order mark that helps Excel recognize UTF-8.

The main benefit of CSV is portability: almost any tool can read it, and you can inspect it in a text editor. The main tradeoff is that CSV stores only text, so column types, factor levels, time zones and anything more complex than a flat table are lost or must be reconstructed when the file is read back. If the data only needs to move between R sessions, a native format such as RDS keeps types intact and is usually the better choice. A columnar format such as Parquet is a common alternative for large data or for exchange with other analytics tools.

The main functions compared

Function | Package | Row names by default | Missing values written as | Encoding behavior | Notes
write.csv() | base R | Included | NA | Native encoding unless fileEncoding is set | Comma separator, period decimal mark; separator and decimal mark cannot be changed
write.csv2() | base R | Included | NA | Native encoding unless fileEncoding is set | Semicolon separator, comma decimal mark, for locales that use a decimal comma
write.table() | base R | Included | NA | Native encoding unless fileEncoding is set | Fully configurable; use for TSV or custom delimiters
write_csv() | readr | Not written | NA | Always UTF-8, no byte order mark | Quotes only where needed by default
write_excel_csv() | readr | Not written | NA | UTF-8 with byte order mark | Aimed at files opened in Excel
fwrite() | data.table | Not written | Empty field | UTF-8 text; byte order mark optional via bom = TRUE | Designed for speed on large tables

This table reflects the documented defaults of each function. Defaults can change between package versions, so check the help page for the version you have installed if a detail matters for your workflow.

Basic examples

# Base R: drop row names unless you need them
write.csv(df, "output.csv", row.names = FALSE)

# Base R with explicit encoding
write.csv(df, "output.csv", row.names = FALSE, fileEncoding = "UTF-8")

# readr
library(readr)
write_csv(df, "output.csv")
write_excel_csv(df, "output_for_excel.csv")

# data.table
library(data.table)
fwrite(df, "output.csv")

What exporting actually changes

A data frame in R holds typed columns: numbers, text, logical values, factors, dates and date-times. A CSV file holds only lines of text, with fields separated by commas. Exporting is a conversion from the first to the second, and every typed value is turned into a string. Nothing in the CSV records what type a column was, so the program that reads the file has to guess or be told.

  • Row names: base R functions write them as an extra unnamed first column by default. When the file is read back, this often appears as a column called X or ...1. Setting row.names = FALSE avoids this.
  • Factors: written as their labels. The set of levels and their order is not stored, so unused levels and level ordering are lost.
  • Missing values: written as NA by most functions, or as an empty field by fwrite(). Other tools may treat the literal text NA as a string rather than a missing value, so use the na argument if the destination expects blanks.
  • Dates: Date columns are written in year-month-day form, which most tools parse reliably.
  • Date-times: base R writes them as text in the session's local time without a time zone marker. readr and fwrite() write ISO 8601 text in UTC by default. Either way, the reader must interpret the time zone correctly.
  • Quoting: write.csv() quotes all character and factor columns. write_csv() quotes only fields that contain the separator, quotes or line breaks. Both are valid CSV, but the files will not be byte-for-byte identical.
  • List-columns and nested data: these have no flat text form and must be flattened or converted before export.

Because these changes affect stored content, not just how the file looks, a round trip through CSV can return a data frame that differs from the original even when the export reported no errors. If exact preservation matters, compare the re-imported data with the original rather than assuming it survived.

Will the file remain usable after sharing?

CSV is plain text, so it copies, emails and versions cleanly, and it can be read by assistive technology in any editor or spreadsheet that presents it as text or a grid. The practical problems come from how receiving programs interpret that text.

Character encoding

Accented letters and non-Latin scripts depend on the file being read with the same encoding it was written in. readr always writes UTF-8. Base R writes in the session's native encoding unless fileEncoding is set, and the native encoding varies by platform and R version, so setting fileEncoding = "UTF-8" removes the ambiguity. Some versions of Excel do not detect UTF-8 in a CSV without a byte order mark and display garbled characters; write_excel_csv() or fwrite(bom = TRUE) addresses this. Behavior varies across Excel versions and platforms, and no testing against specific versions was available for this review, so check the result in the version your recipients use.

Decimal marks and separators

In regions that use a comma as the decimal mark, spreadsheets often expect a semicolon as the field separator. A comma-separated file opened in that setting may load as a single column. write.csv2() and readr::write_csv2() produce the semicolon-and-decimal-comma convention. For files shared internationally or consumed by other software, the standard comma-and-period form is usually safer, and recipients can specify the separator on import.

Spreadsheet reformatting

Spreadsheet programs apply their own type detection when opening a CSV. Identifiers with leading zeros, such as postal codes or account numbers, may lose those zeros. Long numeric identifiers may be shown in scientific notation or rounded. Text that resembles a date may be converted to one. These changes happen in the spreadsheet, not in R, and the CSV itself is still correct, but if someone saves the file again from the spreadsheet, the altered values replace the originals. Importing the file through the spreadsheet's import tool and marking those columns as text avoids this.

Exporting TSV instead

Tab-separated output is useful when the data contains many commas, such as free text or addresses, because fewer fields need quoting. Base R has no dedicated wrapper, so use write.table() with a tab separator. readr provides write_tsv(), and fwrite() accepts sep = "\t".

# Base R
write.table(df, "output.tsv", sep = "\t", row.names = FALSE, quote = FALSE)

# readr
readr::write_tsv(df, "output.tsv")

# data.table
data.table::fwrite(df, "output.tsv", sep = "\t")

Setting quote = FALSE in the base R example produces cleaner output but is only safe if no field contains a tab or line break. If that cannot be ruled out, leave quoting on.

When CSV is not the right choice

  • Saving work to reload in R later: saveRDS() and readRDS() keep column types, factor levels and attributes.
  • Large datasets or repeated analytical use: a columnar format such as Parquet, through the arrow package, preserves types and is generally more compact.
  • Deliverables that need formatting, multiple sheets or fixed column types in Excel: a native spreadsheet file, written with a package such as writexl or openxlsx, avoids the type guessing that CSV invites.
  • Nested or hierarchical data: JSON or a database table represents the structure without forced flattening.

Gaps in this review

This review draws on the documented behavior of base R, readr and data.table. It does not include measured speed comparisons, file size figures, or tests of how specific spreadsheet versions or screen readers handle the output. Those depend on your data, package versions and destination software, so a short check with a representative file in the actual target environment is the most reliable way to confirm the export works for your purpose.