It is a common requirement to save our dataframe for portability after working on it on the computer system’s auxiliary memory using the R Programming Language.
In this tutorial, we will export our dataframe to Excel using the writexl package.
writexl Package:
You can export your DataFrame to Excel by using the writexl package in R.
Installation
Type the below command in your R console to install the writexl package.
install.packages("writexl")
Output:
Installing package into ‘C:/Users/cirus/AppData/Local/R/win-library/4.2’ (as ‘lib’ is unspecified) trying URL 'https://cloud.r-project.org/bin/windows/contrib/4.2/writexl_1.4.0.zip' Content type 'application/zip' length 190332 bytes (185 KB) downloaded 185 KB package ‘writexl’ successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\cirus\AppData\Local\Temp\RtmpOwpVEv\downloaded_packages
write_xlsx() Function:
The write_xlsx() function from the writexl package is the simplest way to export a data frame to an Excel file in R.
It is used to write a dataframe to an Excel (.xlsx) file.
Syntax:
write_xlsx(x, path)
Parameters
- x: It is the data frame name to export
- path: It is the path of the file to write to.
Exporting a DataFrame to Excel File in R
Method #1: Exporting the Dataframe to an Excel File
Example1
Approach:
- Import writexl package using the library() function.
- Create the dataFrame that you want to export to Excel file.
- Display the above created dataframe.
- Save the above dataframe in an Excel File by passing the dataframe and excel file path as arguments to the write_xlsx() function.
- The Exit of the Program.
Below is the implementation:
# Import writexl package using the library() function library(writexl) # Create the dataFrame that you want to export to Excel file datafrme <- data.frame(Fruits = c("Apple", "Mango", "Orange", "Grapes"), CostPerKG = c(120, 100, 50, 70)) # Display the above created dataframe datafrme # Save the above dataframe in an Excel File by passing the # dataframe and excel file path as arguments to the write_xlsx() function write_xlsx(datafrme,"Output_dataframe.xlsx")
Output:
Fruits CostPerKG 1 Apple 120 2 Mango 100 3 Orange 50 4 Grapes 70
Explanation:
Here the dataframe is exported to the given directory or path as an Excel file.
Example2
The only difference between this example and the previous ones is the number of arguments supplied.
Here, we have passed two extra arguments that allow us to have more control over the formatting of the Excel file. They are:
col_names: It is used to write column names at the top of the Excel file (.xlsx)
_headers: It is used to make the column names(col_names) in the Excel file centered and bold.
Approach:
- Import writexl package using the library() function
- Create the dataFrame that you want to export to Excel file
- Display the above created dataframe.
- Save the above dataframe in an Excel File by passing the dataframe and excel file path, col_names as TRUE, format_headers as TRUE as arguments to the write_xlsx() function
- Here col_name writes column names at the top of the given Excel file.
- By passing format_headers = TRUE we can make column names centered and bold.
- The Exit of the Program.
Below is the implementation:
# Import writexl package using the library() function library(writexl) # Create the dataFrame that you want to export to Excel file datafrme <- data.frame(Fruits = c("Apple", "Mango", "Orange", "Grapes"), CostPerKG = c(120, 100, 50, 70)) # Display the above created dataframe datafrme # Save the above dataframe in an Excel File by passing the # dataframe and excel file path, col_names as TRUE, format_headers as TRUE # as arguments to the write_xlsx() function # Here col_name writes column names at the top of the given Excel file. # By passing format_headers = TRUE we can make column names centered and bold. write_xlsx(datafrme,"Output_dataframe.xlsx", col_names = TRUE, format_headers = TRUE )
Output:
Fruits CostPerKG 1 Apple 120 2 Mango 100 3 Orange 50 4 Grapes 70
Method #2: Exporting the Dataframe to a CSV File
Because write.csv() is a built-in function in R, we don’t need to install any other libraries to use it.
It is used to write a dataframe to a CSV file(.csv)
Syntax:
write.csv(dataframe, filepath)
Parameters
- dataframe: It is the data frame name to export.
- filepath: It is the path of the csv file to write to. It indicates the location where our CSV file with the given file name will be saved.
Approach:
- Create the dataFrame that you want to export to csv file and store it in a variable
- Display the above created dataframe
- Save the above dataframe in a CSV File by passing the dataframe and csv file path as arguments to the write.csv() function.
- The Exit of the Program.
Below is the implementation:
# Create the dataFrame that you want to export to csv file # and store it in a variable datafrme <- data.frame(Fruits = c("Apple", "Mango", "Orange", "Grapes"), CostPerKG = c(120, 100, 50, 70)) # Display the above created dataframe datafrme # Save the above dataframe in a CSV File by passing the # dataframe and csv file path as arguments to the write.csv() function write.csv(datafrme,"Output_dataframe.csv")
Output:
Fruits CostPerKG 1 Apple 120 2 Mango 100 3 Orange 50 4 Grapes 70