Using the write.csv function in base R, it can export a data frame to a CSV file. The write.csv function takes data frame and path of export to CSV file as the input parameters.

Use the following syntax to write a data frame to the CSV file.

write.csv(dataframe,"path of export data frame to csv file", row.names = FALSE)

R language provides other methods like the write_csv function from the readr package and fwrite function of data.table package to write a data frame to the CSV file.

In this tutorial, we will discuss how to export a data frame to a CSV file in R using the write.csv function and other methods like write_csv and fwrite to write a data frame to a CSV file.

Export a Data Frame to CSV file using write.csv

Using the write.csv function of base R, it can export a data frame to the CSV file.

Let’s understand the exporting of a data frame with an example.

Create a data frame in R

# Create a data frame
student_info <- data.frame(
  name = c("Tom","Kim","Sam","Julie","Emily","Chris"),
  age = c(20,21,19,20,21,22),
  gender = c('M','F','M','F','F','M'),
  marks = c(72,77,65,80,85,87)
)
# Print data frame
student_info

If you run the above R code, it will give the following output.

  name age gender marks
1   Tom  20      M    72
2   Kim  21      F    77
3   Sam  19      M    65
4 Julie  20      F    80
5 Emily  21      F    85
6 Chris  22      M    87

Use the write.csv function to export a data frame to a CSV file.

#Write a data frame to a CSV file
write.csv(student_info,"D:\\student_info.csv",row.names=FALSE)

If you run the above R code, it will write a data frame to the CSV file at the specified location.

The output of the above R code after exporting the data frame to CSV is:

Write data frame to CSV file

Write data frame to CSV file

Write a Data Frame to CSV using write_csv from readr package

Using the write_csv function from the readr package, you can export a data frame to a CSV file.

write_csv function is 2x faster than write.csv function.

write_csv function syntax:

write_csv(
  x,
  file,
  na = "NA",
  append = FALSE,
  col_names = !append,
  quote = c("needed", "all", "none"),
  escape = c("double", "backslash", "none"),
  eol = "\n",
  num_threads = readr_threads(),
  progress = show_progress(),
  path = deprecated(),
  quote_escape = deprecated()
)

Let’s consider an example to illustrate the export of data frame to the CSV file using the write_csv function.

Create a data frame in R using data.frame() function.

# Create a data frame
emp_info <- data.frame(
  id = c(1,2,3,4,5,6),
  name = c("Henry","Gary","Sam","Julie","Kim","Chris"),
  age = c(28,24,29,25,23,24),
  salary = c(4500,4000,5500,3650,3400,3800)
)
# Print data frame
emp_info

Run the above R code, it will print the data frame as the following.

  id  name age salary
1  1 Henry  28   4500
2  2  Gary  24   4000
3  3   Sam  29   5500
4  4 Julie  25   3650
5  5   Kim  23   3400
6  6 Chris  24   3800

Load library readr to use write_csv function. The write_csv function writes a data frame to a CSV file.

# load readr library
library(readr)

# Use write_csv function to export data frame to csv
write_csv(emp_info,"D:\\emp_info.csv")

When you run the above R code, it writes a data frame to a CSV file at the specified location.

The output of the data frame written to CSV file is:

Export a data frame to CSV

Export a data frame to CSV

Usint fwrite to Export a Data Frame to CSV file

The fwrite function from the data.table package is very powerful to export a large data frame to a CSV file. It is 2x powerful than the write_csv function of the readr package.

Let’s practice using the fwrite function to export a data frame to a CSV file.

Create a data frame in R.

# Create a data frame
sales_data <- data.frame(
  id = c(1,2,3,4,5,6),
  name = c("Ebook","Audio","Video","Course","Book","Software"),
  revenue = c(45500,53000,55200,13650,33400,13800)
)
# Print data frame
sales_data

If you run the above R code, it will print the data frame as follows.

  id     name revenue
1  1    Ebook   45500
2  2    Audio   53000
3  3    Video   55200
4  4   Course   13650
5  5     Book   33400
6  6 Software   13800

Use the fwrite function to write a data frame to a CSV file.

# Use the fwrite function to export a data frame to csv file
fwrite(sales_data,"D:\\sales_data.csv")

The output of the above fwrite function to write a data frame data to a CSV file is:

fwrite- write dataframe to csv file

fwrite- write dataframe to csv file

Conclusion

I hope the above article on how to export a data frame to a CSV file is helpful to you.

Using the write.csv function of base R, you can write a data frame to a CSV file.

The write_csv function from the readr package is very powerful and 2x faster as compared to the write.csv function and it also exports a data frame to a CSV file.

fwrite function performs writing a data frame to CSV file very fastly and 2x faster than write_csv function. It is recommended to use for large data frames.