The “write.xlsx” Function in R
Package: openxlsx
Purpose: Writes data frames or matrices to an Excel file.
General Class: Data Export
Required Argument(s):
x: Data frame or matrix to be written to an Excel file.
file: The file path where the Excel file will be saved.
Notable Optional Arguments:
sheet: The name of the Excel sheet to write the data to.
colNames: Logical. Whether to include column names in the Excel file.
...: Additional arguments that can be passed to customize the Excel output.
Example:
# Example data for using the write.xlsx-like function
library(openxlsx)
# Create a sample data frame
data <- data.frame(Name = c("John", "Jane", "Bob"),
Age = c(25, 30, 22),
Salary = c(50000, 60000, 45000))
# Specify the file path
file_path <- "example.xlsx"
# Write the data frame to an Excel file
write.xlsx(data, file = file_path, sheet = "Sheet 1", colNames = TRUE)In this example, the write.xlsx function from the openxlsx package is used to write a data frame (data) to an Excel file specified by the file path (file_path). The sheet name is specified as “Sheet 1,” and column names are included in the Excel file. The openxlsx package provides flexible options for customizing the Excel output.