Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

How to Create Multiple Sheets in Excel workbook Using R

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In this article, we will discuss how to create multiple sheets in an excel file using the xlsx package. As we all know in general an excel file might contain one or more than one sheet present in it. 

Manually we can create and insert data into multiple sheets in Excel GUI Application but when it comes to R Programming by default the data is inserted into Sheet1 but we can also modify it. 

This can be achieved by specifying the sheet names in the write.xlsx() function where we want to insert data.

Syntax:  write.xlsx(df, file, sheetName, col.names, row.names, append, showNA, password)


  • df – data frame to be converted
  • file – the path to the output excel file is specified here
  • sheetName – to string is passed as sheet name
  • col.names – logical value indicating if the column names of the data frame are to be written in the file
  • row.names – logical value indicating if the column names of the data frame are to be written in the file
  • append – when one wants to append the data to an existing file then this value needs to be marked as TRUE
  • showNA- If TRUE NA(Null) Will be shown otherwise will leave as empty cells in the excel sheet

Importing Libraries

In this article, we will be requiring xlsx package to create the dataset. 


# install the xlsx package
# load the package into working environment

Creating dataset and writing in Excel File

In this step we will add as many dataset we want and write them in the excel file. We just need to change the sheetname and it will be done.


# Creation of a sample data frame
# Writing the data frame into an excel file including NULL(NA) values
                 col.names = TRUE,showNA=TRUE,append=TRUE)

Now we can add one more data frame in the excel file.


# Creation of another sample data frame
df2 <- data.frame(Roll_Number = c(1,2,3,4,5,6,7,8,9,10),
                         Age= c(18,19,20,19,18,17,18,19,17,16),
                         Marks  = c(98,96,89,90,75,35,88,92,78,94))
# Inserting another data frame in another sheet of excel file

Similarly, we can more sheets to the file. Let’s see the output of the above code.




The output of the second sheet.



My Personal Notes arrow_drop_up
Last Updated : 30 Dec, 2022
Like Article
Save Article
Similar Reads
Related Tutorials