The writexl package can be used to export a 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
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 Age
1 Jon 23
2 Bill 41
3 Maria 32
4 Ben 58
5 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 |
Jon | 23 |
Bill | 41 |
Maria | 32 |
Ben | 58 |
Tina | 26 |
You may check the following guide for the steps to import an Excel file into R.