Write R output to CSV
How to write R output to a CSV or TSV file with write.csv, write.table and readr, what the export changes about your data, and when CSV is the wrong choice.
Writing R output to a CSV file turns an in-memory object, usually a data frame, into delimited plain text that spreadsheets, databases and other languages can open. This review covers what that conversion keeps, what it discards, and when a different format is a better fit. No hands-on tests were supplied for this article. Function behavior described here comes from R's documented defaults, and anything that depends on a specific spreadsheet version or locale is flagged as something to check yourself.
Is CSV export suitable for your use?
CSV is a good fit when your output is a rectangular table and the recipient needs to open it in another tool. Its main benefit is portability: almost any spreadsheet, database loader or scripting language can read it, and you can inspect it in a text editor.
The main tradeoff is that CSV stores only text. Column types, factor levels, time zones, attributes and nested structures are not kept as R objects. They are either converted to strings or lost. When the file is read back, the reading program has to guess the types again.
- Prefer another format when:
- The output is not a table, such as a model summary, a list or printed console output. Convert it to a data frame first, or save it as plain text.
- You will reload the data in R and need exact types. Use saveRDS() and readRDS().
- The data is large or will be exchanged between analytical tools. A typed columnar format such as Parquet, via the arrow package, preserves types and is generally more compact.
- Cells contain lists or nested data. Flatten them before export.
The main ways to write a CSV from R
| Function | Package | Delimiter / decimal | Row names by default | Notes |
| write.csv() | base R (utils) | comma / period | Yes | Quotes character and factor columns by default |
| write.csv2() | base R (utils) | semicolon / comma | Yes | For locales that use a comma as the decimal mark |
| write.table() | base R (utils) | any, set with sep | Yes | General form; use sep = "\t" for TSV |
| write_csv() | readr | comma / period | No | Writes UTF-8; quotes values only when needed |
| write_tsv() | readr | tab / period | No | TSV counterpart of write_csv() |
| write_excel_csv() | readr | comma / period | No | Adds a UTF-8 byte order mark so spreadsheet software detects the encoding |
| fwrite() | data.table | comma by default, set with sep | No | Designed for speed on large tables |
# Base R: drop row names unless you need them as a column
write.csv(df, "output.csv", row.names = FALSE)
# Base R, tab-separated
write.table(df, "output.tsv", sep = "\t", row.names = FALSE, quote = FALSE)
# readr
library(readr)
write_csv(df, "output.csv", na = "")
write_excel_csv(df, "output_for_excel.csv")
The row.names = FALSE argument matters with base R. If you leave it out, write.csv() adds an unnamed first column of row names, often just 1, 2, 3. That column shows up as extra data when the file is reopened.
What the export actually changes
Exporting to CSV is a stored-character change: each value is converted to text and written with delimiters and line breaks. Nothing about how the file is displayed is controlled by the file itself. Any later formatting, such as dates shown in a regional style or numbers shown with fewer decimals, comes from the program that opens it.
- Specific changes to expect:
- Factors are written as their labels. The level order and any unused levels are not stored.
- Dates are written as text, typically in year-month-day form. Date-times are also written as text; readr writes them in ISO 8601 form. Check how time zones are represented before relying on them.
- Doubles are converted to decimal text. If exact numeric round-tripping matters, compare a reloaded file against the original.
- Missing values are written as NA by default. Set the na argument to "" if the destination expects empty cells.
- Fields that contain the delimiter, quotes or line breaks are quoted, and embedded quotes are escaped. This keeps the file parseable, but simple line-by-line tools may split such records incorrectly.
- List columns cannot be written as normal cells. Flatten or drop them first.
Model output, test results and similar objects are not tables. A common approach is to extract the parts you need into a data frame first, for example with broom::tidy() for many model types, and then write that data frame. capture.output() saves printed console text, but the result is plain text, not a structured CSV.
library(broom)
fit <- lm(mpg ~ wt + hp, data = mtcars)
write.csv(tidy(fit), "model_coefficients.csv", row.names = FALSE)
Will the file stay readable after sharing?
Most problems after sharing a CSV come from three sources: encoding, regional settings and type guessing in the program that opens it.
- Encoding: non-ASCII characters such as accented letters can appear garbled if the reading program assumes a different encoding. readr writes UTF-8. With base R, set fileEncoding = "UTF-8". If recipients open the file directly in a spreadsheet, write_excel_csv() adds a byte order mark intended to help detection. This article has not tested how specific spreadsheet versions handle this, so check with your recipients' software.
- Regional settings: in locales where the comma is the decimal mark, spreadsheet software may expect semicolon-separated files. write.csv2() produces that layout. Agree on a convention with recipients instead of assuming one.
- Type guessing: spreadsheets may reinterpret text. Long ID numbers, codes with leading zeros, and strings that look like dates are common casualties. The CSV itself is still correct, but opening and resaving it in a spreadsheet can change the values. For such columns, import them as text explicitly instead of double-clicking the file.
For accessibility, a CSV holds only data. It has no headings, captions or other structure that a screen reader could use. Clear, unambiguous column names are the main thing you control. If people, rather than programs, will read the results, a formatted report or an accessible HTML table is a better deliverable, with the CSV attached as the machine-readable copy.
Recommendation and evidence gaps
For a data frame that needs to reach other tools, writing a CSV is the practical default. Use write.csv() with row.names = FALSE, or readr::write_csv() if you want UTF-8 output and no row names by default. Use a TSV when your text fields often contain commas. Use saveRDS() or Parquet when types must survive a round trip. Convert non-tabular output to a data frame first.
Gaps: no performance measurements, file-size comparisons or rendering tests in particular spreadsheet applications were supplied. Behavior in specific destinations, and speed differences between base R, readr and data.table, should be confirmed on your own data and tools.