HOWTO · R
How to Export Data Frame to CSV in R
Learn how to export an R data frame to CSV with write.csv(), write.table(), data.table::fwrite(), and readr::write_csv().
On this page
Use Base R’s write.csv() to save a data frame as a comma-separated file, or use write.table() when you want to set the delimiter yourself. The data.table::fwrite() and readr::write_csv() functions are package alternatives. The examples below use one small data frame and explain where each file is saved and whether row names are included.
Export a data frame with write.csv() and write.table()
The following Base R example writes both formats into R’s temporary directory and checks that the files exist. Its recorded run completed successfully, and both file-existence checks returned TRUE:
people <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(25, 30, 35),
City = c("New York", "Los Angeles", "Chicago")
)
csv_path <- file.path(tempdir(), "people.csv")
table_path <- file.path(tempdir(), "people-table.csv")
write.csv(people, file = csv_path, row.names = FALSE)
write.table(people, file = table_path, sep = ",", row.names = FALSE, col.names = TRUE)
file.exists(c(csv_path, table_path))
[1] TRUE TRUE
file.path(tempdir(), ...) makes the output location explicit and avoids relying on the current working directory. For a permanent destination, replace the temporary paths with a file name such as "people.csv" to write in the working directory, or supply a full path. Forward slashes work in Windows paths, for example "C:/Users/you/Documents/people.csv"; file.path() can also assemble path components for the current operating system.
write.csv() is a convenience form of write.table() configured for comma-separated data. Both functions include row names by default; setting row.names = FALSE omits them, while row.names = TRUE writes them as an extra first field. write.table() lets you choose the separator with sep; use sep = "," for comma-separated data, and retain col.names = TRUE when the first line should contain column headers. The call to file.exists() returns a logical value for each requested path, so it can confirm that the files were created. To inspect their contents, open the files in a text editor or spreadsheet application.
Export a data frame with data.table::fwrite()
The data.table package provides fwrite() for writing a data frame or data table to a delimited file. Install the package once with install.packages("data.table"), then load it with library(data.table) before calling it. For the people data frame above, data.table::fwrite(people, file.path(tempdir(), "people-fwrite.csv")) writes a comma-separated file and omits row names. The namespace form data.table::fwrite() makes the package that supplies the function explicit. The checked run above covers only Base R; this package example is shown as usage guidance and has not been executed.
Export a data frame with readr::write_csv()
The readr package is another option. Install it once with install.packages("readr"), then load it with library(readr) or call the function using its namespace. For the same data, readr::write_csv(people, file.path(tempdir(), "people-readr.csv")) writes comma-separated values without row names and includes the column names. Unlike write.table(), write_csv() does not expose a delimiter argument because it writes CSV. The checked run above covers only Base R; this readr example is shown as usage guidance and has not been executed.