How to Export DataFrame to CSV in R

Here is a template that you may use to export a DataFrame to CSV in R:

write.csv(DataFrame Name, "Path to export the DataFrame\\File Name.csv", row.names=FALSE)

And if you want to include the row.names, simply change it to TRUE.

In the next section, you’ll see an example with the steps to export your DataFrame.

Steps to Export a DataFrame to CSV in R

Let’s say that you have the following dataset:

productprice
computer800
monitor450
keyboard100
printer150

The goal is to export that dataset to a CSV file. But before you do that, you’ll need to capture that data in R in the form of a DataFrame.

Step 1: Create a DataFrame

To create a DataFrame in R, you may use this template:

df <- data.frame(column1 = c("value 1", "value 2", "value 3", ...),
                 column2 = c("value 1", "value 2", "value 3", ...)
                 )

print(df)

Note that it’s not necessary to place quotes around numeric values.

For our example, you’ll get:

df <- data.frame(product = c("computer", "monitor", "keyboard", "printer"),
                 price = c(800, 450, 100, 150)
                 )

print(df)

If you run the code in R, you’ll see the following DataFrame:

   product price
1 computer   800
2  monitor   450
3 keyboard   100
4  printer   150

Step 2: Use write.csv to Export the DataFrame

Next, you’ll need to add the syntax to export the DataFrame to a CSV file in R.

To do that, simply use the template that you saw at the beginning of this guide:

write.csv(DataFrame Name, "Path to export the DataFrame\\File Name.csv", row.names=FALSE)

You’ll need to include the path where you’d like to export the DataFrame on your computer.

For illustration purposes, the following path will be used when exporting the DataFrame (where the file name to be created is ‘my_products’):

C:\\Users\\Ron\\Desktop\\Test\\my_products.csv

So here is the code to export the DataFrame to CSV for our example:

df <- data.frame(product = c("computer", "monitor", "keyboard", "printer"),
                 price = c(800, 450, 100, 150)
                 )

write.csv(df, "C:\\Users\\Ron\\Desktop\\Test\\my_products.csv", row.names=FALSE)

Pay attention to several highlighted portions in the path name:

C:\\Users\\Ron\\Desktop\\Test\\my_products.csv

  • The blue portion represents the file name to be created. For our example, we used the file name of ‘my_products‘ but you may specify another file name if you wish
  • The green portion reflects the file type of .csv. Don’t forget to add that portion when exporting CSV files

Also notice that a double backslash (‘\\’) was used within the path. By adding a double backslash, you would avoid the following error in R:

Error: ‘\U’ used without hex digits in character string starting “”C:\U”

Alternatively, you may use forward slash (‘/’) in each part of the path.

Step 3: Run the code to Export the DataFrame to CSV

Finally, run the code in R (adjusted to your path), and a new CSV file will be created at your specified location.

The data within that file should match with the data in DataFrame created in R:

productprice
computer800
monitor450
keyboard100
printer150

Conclusion

You just saw how to export a DataFrame to a CSV file in R. At times, you may face an opposite situation, where you’ll need to import a CSV file into R.

If that’s the case, you may want to visit the following guide that explains how to import a CSV file into R.

Finally, you may also want to check the Data Output documentation.