How to Write to CSV file in R

03.27.2021

R provide many conveinient ways to export data. One of the most common ways to export data frames is to csv, since dataframes are easily formatted as a table with columns and rows. In this article, we will see how to export a dataframe to CSV in R.

To write to a csv file, we ca use the write_csv. We first need to import the tidyverse library to do so.

library(tidyverse)

Next, we pass our data frame as the first argument to the write_csv function, and the path to the file as the second. The file does not need to exist.

name <- c("Jon", "Bill", "Maria")
age <- c(23, 41, 32)

df <- data.frame(name, age)

write_csv(df, path = "./export.csv")

The finaly result will be a csv file in our local director with the contents of our data frame.