Table of Contents
The write.xlsx function in R allows users to easily export data from R into an Excel spreadsheet. This function requires the user to specify the name of the data object they wish to export, as well as the desired file path and name for the Excel file. Additionally, users have the option to include column names and row names in the exported spreadsheet. This function is useful for organizing, analyzing, and sharing data with others who may not have access to R. It is a simple and efficient way to transfer data between the two platforms.
Use write.xlsx in R (With Examples)
You can use the write.xlsx function in R to write a data frame to an Excel workbook.
This function uses the following basic syntax:
write.xlsx(x, file, sheetName = "Sheet1", ...)
where:
- x: Name of data frame
- file: path to output file
- sheetName: Sheet name to appear in workbook. Default is “Sheet1”
The following step-by-step example shows how to use the write.xlsx function in practice.
Step 1: Install & Load xlsx Package
First, we must install and load the xlsx package in order to use the write.xlsx function:
install.packages('xlsx') library(xlsx)
Step 2: Create the Data Frame
Next, let’s create the following data frame in R:
#create data frame df <- data.frame(team=c('A', 'B', 'C', 'D', 'E'), points=c(99, 90, 86, 88, 95), assists=c(33, 28, 31, 39, 34), rebounds=c(30, 28, 24, 24, 28)) #view data frame df team points assists rebounds 1 A 99 33 30 2 B 90 28 28 3 C 86 31 24 4 D 88 39 24 5 E 95 34 28
Step 3: Use write.xlsx to Export Data Frame to Excel File
Next, let’s use write.xlsx() to write the data frame to a file called my_data.xlsx:
#write data frame to Excel file write.xlsx(df, 'my_data.xlsx')
The file will automatically be written into the current .
If I navigate to the current working directory, I can find this Excel file:

The values in the Excel workbook match those from the data frame.
Step 4 (Optional): Use write.xlsx with Custom Arguments
Note that you can also use the following syntax to change the sheet name in the Excel workbook and suppress the row names:
#write data frame to Excel file write.xlsx(df, 'my_data.xlsx', sheetName = 'basketball_data', row.names=FALSE)
If I navigate to the current working directory, I can find this Excel file:

Notice that the sheet name has changed and the first column no longer contains the row numbers.
Additional Resources
The following tutorials explain how to export other types of files in R:
Cite this article
stats writer (2024). How can I use the write.xlsx function in R to export data into an Excel spreadsheet?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-the-write-xlsx-function-in-r-to-export-data-into-an-excel-spreadsheet/
stats writer. "How can I use the write.xlsx function in R to export data into an Excel spreadsheet?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-the-write-xlsx-function-in-r-to-export-data-into-an-excel-spreadsheet/.
stats writer. "How can I use the write.xlsx function in R to export data into an Excel spreadsheet?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-the-write-xlsx-function-in-r-to-export-data-into-an-excel-spreadsheet/.
stats writer (2024) 'How can I use the write.xlsx function in R to export data into an Excel spreadsheet?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-the-write-xlsx-function-in-r-to-export-data-into-an-excel-spreadsheet/.
[1] stats writer, "How can I use the write.xlsx function in R to export data into an Excel spreadsheet?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I use the write.xlsx function in R to export data into an Excel spreadsheet?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
