How do I export data frames to multiple Excel sheets using R? 2

How do I export data frames to multiple Excel sheets using R?

Exporting data frames to multiple Excel sheets using R is a process that allows users to efficiently organize and transfer their data from R to Excel. This method is particularly useful for individuals who work with large amounts of data and need to present it in a more organized and visually appealing format. Using R, users can easily create and manipulate multiple data frames and then export them to separate sheets within a single Excel file. This allows for better organization and analysis of data, making it a valuable tool for data scientists, researchers, and analysts. By following simple steps, users can easily export their data frames to multiple Excel sheets and enhance the presentation and interpretation of their data.

R: Export Data Frames to Multiple Excel Sheets


You can use the following basic syntax to export multiple data frames in R to multiple worksheets in Excel:

library(openxlsx)

dataset_names <- list('Sheet1' = df1, 'Sheet2' = df2, 'Sheet3' = df3)
write.xlsx(dataset_names, file = 'mydata.xlsx')

The following example shows how to use this syntax in practice.

Example: Export Multiple Data Frames to Multiple Excel Sheets

Suppose we have the following three data frames in R:

#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))

We can use the following syntax to export all three of these data frames to separate sheets within the same Excel file:

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') 

Once I navigate to the location on my computer where the Excel sheet was exported, I can view each of the data frames in their own sheets within the same Excel file called mydata.xlsx:

Sheet1:

Sheet2:

Sheet3:

Note that we exported three data frames to three separate sheets in Excel in this example, but we can use the same syntax to export any number of data frames we’d like.

Cite this article

stats writer (2024). How do I export data frames to multiple Excel sheets using R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-i-export-data-frames-to-multiple-excel-sheets-using-r/

stats writer. "How do I export data frames to multiple Excel sheets using R?." PSYCHOLOGICAL SCALES, 5 May. 2024, https://scales.arabpsychology.com/stats/how-do-i-export-data-frames-to-multiple-excel-sheets-using-r/.

stats writer. "How do I export data frames to multiple Excel sheets using R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-i-export-data-frames-to-multiple-excel-sheets-using-r/.

stats writer (2024) 'How do I export data frames to multiple Excel sheets using R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-i-export-data-frames-to-multiple-excel-sheets-using-r/.

[1] stats writer, "How do I export data frames to multiple Excel sheets using R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How do I export data frames to multiple Excel sheets using R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top