To export numerous data frames from R to multiple Excel worksheets, use the following basic syntax:
library(openxlsx)
dataset_names <- list('Sheet1' = df1, 'Sheet2' = df2, 'Sheet3' = df3)
write.xlsx(dataset_names, file = 'mydata.xlsx')
Exporting Multiple Data Frames to Multiple Excel Sheets is an example.
Let's pretend we have the following three R data frames:
#define data frames
df1 = data.frame(playerID=c(1, 2, 3, 4),
team=c('A', 'B', 'B', 'C'))
df2 = data.frame(playerID=c(1, 2, 3, 4),
rebounds=c(7, 8, 8, 14))
df3 = data.frame(playerID=c(1, 2, 3, 4),
points=c(19, 22, 25, 29))
To export all three of these data frames to distinct sheets within the same Excel file, use the following syntax:
library(openxlsx)
#define sheet names for each data frame
dataset_names <- list('Sheet1' = df1, 'Sheet2' = df2, 'Sheet3' = df3)
#export each data frame to separate sheets in same Excel file
openxlsx::write.xlsx(dataset_names, file = 'mydata.xlsx')