This post will go over how to use the R Programming Language to write data into Excel.
To write data into Excel, we must use the package xlsx of R.
xlsx package:
The excel package is a robust Java-based language for reading, writing, and updating data from and to excel files.
Syntax:
write.xlsx(data, file, sheetName, col.names, row.names, append)
Parameters
data: It is the dataframe that must be written to an excel file.
file: It is the output path where the excel file must be saved
sheetName: It is the sheet name of our choice.
col.names, row.names: This is a boolean value. If true, the row and column names are copied to the Excel spreadsheet.
append: This is a boolean value. If True, data will be appended to the existing excel file.
Installation of xlsx package
Type the below command in the R console:
install.packages("xlsx")
Output:
Installing package into ‘C:/Users/vicky/AppData/Local/R/win-library/4.2’ (as ‘lib’ is unspecified) also installing the dependencies ‘rJava’, ‘xlsxjars’ trying URL 'https://cloud.r-project.org/bin/windows/contrib/4.2/rJava_1.0-6.zip' Content type 'application/zip' length 1245703 bytes (1.2 MB) downloaded 1.2 MB trying URL 'https://cloud.r-project.org/bin/windows/contrib/4.2/xlsxjars_0.6.1.zip' Content type 'application/zip' length 9485708 bytes (9.0 MB) downloaded 9.0 MB trying URL 'https://cloud.r-project.org/bin/windows/contrib/4.2/xlsx_0.6.5.zip' Content type 'application/zip' length 375143 bytes (366 KB) downloaded 366 KB package ‘rJava’ successfully unpacked and MD5 sums checked package ‘xlsxjars’ successfully unpacked and MD5 sums checked package ‘xlsx’ successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\vicky\AppData\Local\Temp\Rtmp84RZij\downloaded_packages
Writing Data Into Excel Using R Language
Here use the built-in iris dataset and save the existing dataset in an excel file called sampleExcelFile (1).xlsx and a sheet name called irisDataValues
Approach:
- Import xlsx(excel) package using the library() function.
- Store the iris data set values into a data frame.
- Pass the above dataframe, output file path and the sheet name of your choice as arguments to the write.xlsx() function to write the above iris dataframe into an existing excel file.
- The Exit of the Program.
Below is the implementation:
# Import xlsx(excel) package using the library() function library(xlsx) # Store the iris data set values into a data frame irisdf <- datasets::iris # Pass the above dataframe, outputfile path and the sheet name of your choice # as arguments to the write.xlsx() function to write the above iris dataframe # into an existing excel file write.xlsx(irisdf, file = "C:/Users/vicky/Downloads/sampleExcelFile (1).xlsx", sheetName="irisDataValues")
Output:
Using The append Argument
Let’s utilize the same file and the append
argument to add another dataset to a different sheet.
Here we use another inbuilt dataset called mtcars for this purpose and append it to the existing file as indicated in the code below.
Approach:
- Import xlsx(excel) package using the library() function
- Store the mtcars data set values into a data frame
- Pass the above dataframe, outputfile path and the sheet name of your choice, append=TRUE as arguments to the write.xlsx() function to write the above mtcars dataframe into an existing excel file
- Here If we give append=TRUE, then the data will be appended to the existing excel file.
- The Exit of the Program.
Below is the implementation:
# Import xlsx(excel) package using the library() function library(xlsx) # Store the mtcars data set values into a data frame mtcarsdf <- datasets::mtcars # Pass the above dataframe, outputfile path and the sheet name of your choice, # append=TRUE as arguments to the write.xlsx() function to write the above # mtcars dataframe into an existing excel file # Here If we give append=TRUE, then the data will be appended to the existing excel file. write.xlsx(mtcarsdf, file = "C:/Users/vicky/Downloads/sampleExcelFile (1).xlsx", sheetName="mtcarsDataValues", append=TRUE)
Output:
Here We appended new Excel sheet using r.