Export R data frames and datasets to CSV

How to export a data frame from R to CSV with write.csv, readr and data.table: what changes in the file, the defaults that cause problems, and when CSV is the wrong choice.

Exporting a data frame from R to CSV turns a typed, in-memory table into plain delimited text. That makes the data portable, but some information is lost along the way. This guide covers the common ways to export a dataset from R to CSV, what the conversion changes, and how to check that the file still works for its intended use. It is based on documented function behavior and general principles. No test results for specific destinations were supplied, so those cases are flagged as things to verify yourself.

Is CSV the right export format for your use?

CSV is the most widely accepted way to hand tabular data to another tool, person or system. Spreadsheets, databases, other languages and most data platforms can read it, and it stays human-readable in any text editor. The cost is that CSV stores only text. Column types, factor levels, attributes and variable labels are not kept, so the receiving program has to guess or be told what each column means.

CSV is a good fit when:

  • The data is a flat table of numbers, text and dates.
  • The recipient uses a tool other than R.
  • You need a format that will still be readable long after the software you used is gone.
  • You want changes to show up cleanly in version control.

Use another format when:

  • You only need to save and reload data within R. saveRDS() and readRDS() preserve types and attributes exactly.
  • The data has list columns, nested structures or labelled survey variables that must survive.
  • The file will be large and read repeatedly by analytical tools. A typed columnar format such as Parquet, via the arrow package, avoids re-parsing text.
  • The recipient specifically needs a spreadsheet file with formatting or multiple sheets.

The main ways to export a data frame to CSV in R

Base R needs no extra packages. Set row.names = FALSE unless you want row names as a column. If you leave the default, write.csv adds an extra first column with an empty header, which surprises many readers of the file.

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

# Semicolon separator and comma decimal mark,
# common in locales that use a decimal comma
write.csv2(df, "output.csv", row.names = FALSE)

# Full control, e.g. tab-separated output
write.table(df, "output.tsv", sep = "\t", row.names = FALSE, quote = FALSE)

The readr package, part of the tidyverse, never writes row names and always writes UTF-8. Its write_excel_csv() variant adds a byte order mark, which helps Excel recognise the file as UTF-8.

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

The data.table package's fwrite() is designed for speed on large tables and also skips row names by default.

library(data.table)
fwrite(df, "output.csv")
fwrite(df, "output.tsv", sep = "\t")

A relative path such as "output.csv" is resolved against the current working directory, which getwd() reports. Use a full path if you are unsure where the file will end up.

Function | Package | Row names by default | Missing values written as | Separator
write.csv | base R | Yes | NA | comma
write.csv2 | base R | Yes | NA | semicolon, with comma decimals
write_csv | readr | No | NA | comma
write_excel_csv | readr | No | NA | comma, with UTF-8 byte order mark
fwrite | data.table | No | empty field | comma

Missing values are handled differently across these functions. Both base R and readr let you change the output with the na argument, for example na = "" for empty fields. Choose whatever the receiving system expects, because a literal NA string can be read back as text instead of as a missing value.

What the export actually changes

This conversion changes how the data is stored, not just how it looks. Every value is written as characters, and the file no longer says what type each column was. In practice:

  • Factors become their text labels. The set of levels and their order are lost, including any unused levels.
  • Dates and date-times become text. readr and data.table write ISO 8601 by default. Time zone information may be converted or dropped depending on the function, so check this if it matters.
  • Numbers become decimal text. Functions differ in how many digits they write, so exact round-tripping of floating-point values is not guaranteed by every method.
  • Attributes, variable labels and value labels, such as those from haven-imported survey data, are not written.
  • List columns cannot be represented in a flat CSV and must be flattened or removed first.
  • Row names are either written as a column or discarded. They are never kept as row names.

Text values that contain the separator, line breaks or double quotes are wrapped in quotes, and any embedded double quotes are doubled. Base R quotes all character and factor columns by default. readr and data.table quote only when needed. Both styles are valid CSV and are read correctly by standard parsers.

Will the file stay readable and usable after sharing?

The file itself is plain text. Most problems come from how the receiving program interprets it.

  • Encoding: readr writes UTF-8. Base R writes in the session's native encoding unless you pass fileEncoding = "UTF-8". Accented or non-Latin characters can appear garbled if the reader assumes a different encoding.
  • Separator and locale: in regions that use a decimal comma, spreadsheet software may expect semicolon-separated files. Use write.csv2 for those recipients. Otherwise a comma file may open as a single column.
  • Spreadsheet reinterpretation: when a CSV is opened directly in a spreadsheet, it may drop leading zeros from codes such as ZIP codes, convert long ID numbers to scientific notation, or reformat date-like strings. These changes happen in the spreadsheet, not in the file, and can become permanent if the file is saved again. Importing with explicit column types avoids this.
  • Editing: because CSV is plain text, it can be edited and compared in any text editor or version control system without special tools.
  • Accessibility: a CSV has no headings, captions or formatting. How accessible it is depends entirely on the program used to open it. Clear, descriptive column names help anyone reading it, including people using screen readers, but no specific assistive technology was tested for this guide.

Behavior in particular spreadsheet versions, databases or upload portals was not verified here. If the file is going to a specific destination, open a sample there before relying on it.

Checking that nothing important was lost

A quick round trip shows what the export changed. Read the file back and compare it with the original. Differences in column types are expected. Differences in values are what you need to investigate.

write.csv(df, "output.csv", row.names = FALSE)
back <- read.csv("output.csv")

str(df)
str(back)
all.equal(df, back, check.attributes = FALSE)

If factors, dates or labels must survive for later R work, keep an RDS copy next to the CSV. The CSV then serves outside users, and the RDS remains the exact record.