How to Export a DataFrame to Excel in R – Data to Fish (2024)

March 15, 2024

The writexl package can be used to export a DataFrame to Excel in R:

Steps to Export a DataFrame to Excel in R

Step 1: Install the writexl package

Type the following command in the R console to install the writexl package:

install.packages("writexl")

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 a DataFrame

Next, create a DataFrame that you wish to export to Excel.

For example, let’s create a simple DataFrame with the following values:

df <- data.frame(Name = c("Jon", "Bill", "Maria", "Ben", "Tina"), Age = c(23, 41, 32, 58, 26) )print(df)

This is how the DataFrame would look like in R:

 Name Age1 Jon 232 Bill 413 Maria 324 Ben 585 Tina 26

Step 3: Export the DataFrame to Excel in R

You may use the following template to export the above 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
  • For demonstration, let’s assume that the path to store the Excel file is: C:\\Users\\Ron\\Desktop\\Test\\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", "Ben", "Tina"), Age = c(23, 41, 32, 58, 26) )write_xlsx(df, "C:\\Users\\Ron\\Desktop\\Test\\people.xlsx")

Once you run the code in R (adjusted to your path), the DataFrame would be exported to your specified location:

Name Age
Jon23
Bill41
Maria32
Ben58
Tina26

You may check the following guide for the steps to import an Excel file into R.

How to Export a DataFrame to Excel in R – Data to Fish (2024)
Top Articles
Latest Posts
Article information

Author: Dr. Pierre Goyette

Last Updated:

Views: 5896

Rating: 5 / 5 (50 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Dr. Pierre Goyette

Birthday: 1998-01-29

Address: Apt. 611 3357 Yong Plain, West Audra, IL 70053

Phone: +5819954278378

Job: Construction Director

Hobby: Embroidery, Creative writing, Shopping, Driving, Stand-up comedy, Coffee roasting, Scrapbooking

Introduction: My name is Dr. Pierre Goyette, I am a enchanting, powerful, jolly, rich, graceful, colorful, zany person who loves writing and wants to share my knowledge and understanding with you.