how to create an excel in r

To create an Excel file in R, we need to use a package called "openxlsx". Follow the below steps to create an Excel file in R:

  1. Install the "openxlsx" package using the following command:
main.r
install.packages("openxlsx")
29 chars
2 lines
  1. Load the "openxlsx" package using the following command:
main.r
library(openxlsx)
18 chars
2 lines
  1. Create a data frame in R with the required data that you want to write to the Excel file.
main.r
data <- data.frame(col1 = c(1, 2, 3), col2 = c("A", "B", "C"), col3 = c(TRUE, FALSE, TRUE))
92 chars
2 lines
  1. Create an Excel workbook and add a worksheet using the following command:
main.r
wb <- createWorkbook("myExcelFile.xlsx")
addWorksheet(wb, "Sheet1")
68 chars
3 lines
  1. Write the data frame to the Excel worksheet using the following command:
main.r
writeDataTable(wb, "Sheet1", data)
35 chars
2 lines
  1. Save the Excel file using the following command:
main.r
saveWorkbook(wb, "myExcelFile.xlsx", overwrite = TRUE)
55 chars
2 lines

This will create an Excel file named "myExcelFile.xlsx" in your working directory with the required data.

related categories

gistlibby LogSnag