To export multiple dataframes to multiple excel sheets, you can use write.xlsx() function from openxlsx library.
The following method shows how you can do it with syntax.
Method: Use write.xlsx() Function
library(openxlsx)
dataset_ <- list('Sheet1' = df1, 'Sheet2' = df2, 'Sheet3' = df3,...)
# Export each data frame to separate sheets in same Excel file
openxlsx::write.xlsx(dataset_, file = 'file.xlsx')
The following example shows how export dataframes to multiple excel sheets in R.
Using write.xlsx() Function
Let’s see we have 3 dataframe and we want to export them in 3 different excel sheets:
library(openxlsx)
# Create data frames
df1 = data.frame(Temperature=c(21, 22, 23, 24),
Machine_=c('A', 'B', 'C', 'D'))
df2 = data.frame(Pressure=c(91, 92, 93, 94),
Machine_=c('A', 'B', 'C', 'D'))
df3 = data.frame(Status=c(TRUE, FALSE, TRUE, FALSE),
Machine_=c('A', 'B', 'C', 'D'))
# Define sheet names for each data frame
dataset_ <- list('Sheet1' = df1, 'Sheet2' = df2, 'Sheet3' = df3)
# Export each data frame to separate sheets in same Excel file
openxlsx::write.xlsx(dataset_, file = 'sample1.xlsx')
In the above code we export 3 different dataframe to 3 sheets of excel using write.xlsx() function from openxlsx package.