How to Export DataFrame to CSV in R

Looking to export a DataFrame to CSV in R?

If so, you’ll see how to accomplish this task using a simple example.

To start, here is a template that you may use to export a DataFrame to CSV in R:

write.csv(Your DataFrame,"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(Your DataFrame,"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. Alternatively, you may use the file type of ‘.txt’ to export a DataFrame to a text file if needed.

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.