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:

name age
Jon 23
Bill 41
Maria 32
Tom 55
Emma 40

The goal is to export that dataset to a CSV file. But before you do that, let’s 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(name = c("Jon", "Bill", "Maria", "Tom", "Emma"),
                 age = c(23,41,32,55,40)
                 )
print(df)

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

   name  age
1   Jon   23
2  Bill   41
3 Maria   32
4   Tom   55
5  Emma   40

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 ‘People’):

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

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

df <- data.frame(name = c("Jon", "Bill", "Maria", "Tom", "Emma"),
                 age = c(23,41,32,55,40)
                 )

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

Pay attention to several highlighted portions in the path name:

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

  • The blue portion represents the file name to be created. In our example, we used the file name of ‘People‘ 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”

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:

name age
Jon 23
Bill 41
Maria 32
Tom 55
Emma 40

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.