Write Data Into Excel Using R
Last Updated :
13 Jun, 2022
In this article, we will discuss how to write data into excel using R Programming Language.
To write data into excel, we must leverage a package called xlsx in R. The excel package is a java based language that is powerful to read, write and update data from and to excel files.
Syntax: write.xlsx(data, file, sheetName, col.names, row.names, append)
Parameters:
- data – the dataframe to be written to excel file
- file – the output path in which the excel file has to be stored
- sheetName – desired name for the sheet
- row.names, col.names – (bool) if true the row and column names are copied to the excel file
- append – if True data will be appended to the existing excel file
Let us use the inbuilt iris dataset and store the existing dataset in the excel file with the name iris.xlsx and sheet name iris_data.
R
install.packages ( "xlsx" )
library ( "xlsx" )
write.xlsx (iris, file = "iris.xlsx" ,
sheetName= "iris_data" )
|
Output:
Output
Now let us use the same file and use the append parameter to append another dataset in a separate sheet. For this purpose, let us use another inbuilt dataset called mtcars and append it to the existing file as shown in the below code.
R
write.xlsx (mtcars, file = "iris.xlsx" ,
sheetName= "mtcars_data" , append= TRUE )
|
Output:
Output