How to Export a DataFrame to Excel File in R

You can use the writexl package in order to export your DataFrame to Excel in R:

library("writexl")
write_xlsx(the dataframe name,"path to store the Excel file\\file name.xlsx")

Steps to Export a DataFrame to Excel in R

Step 1: Install the writexl package

You may type the following command in the R console in order to install the writexl package:

install.packages("writexl")

This is how the command would look like in the R console:

Install the writexl package in R

Follow the instructions to complete the installation. You may check the following guide for the steps to install a package in R.

Step 2: Create the DataFrame

Next, create the DataFrame that you’d like to export to Excel.

For example, I created a simple DataFrame with the following values:

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

This is how the DataFrame would look like in R:

DataFrame in R

Step 3: Export the DataFrame to Excel in R

You may use the following template to assist you in exporting the DataFrame to Excel in R:

library("writexl")
write_xlsx(the dataframe name,"path to store the Excel file\\file name.xlsx")

For our example:

  • The DataFrame name is: df
  • The path that I used to store the Excel file is: C:\\Users\\Ron\\Desktop\\people.xlsx
    • Where the file name to be created is people and the Excel file extension is xlsx

You’ll need to modify the path to reflect the location where you’d like to store the Excel file on your computer. You may also place double backslash (‘\\’) within the path name to avoid any errors in R.

Here is the complete code for our example:

library("writexl")

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

write_xlsx(df,"C:\\Users\\Ron\\Desktop\\people.xlsx")

And this is how the code would look like in the R Editor. Don’t forget to include your DataFrame name (as highlighted in red below):

How to Export a DataFrame to Excel File in R

Once you run the code in R, the DataFrame would then get exported to your specified location:

How to Export a DataFrame to Excel File in R