R

Saving lists to excel tabs

You can save each cut of the diamonds dataset to a different tab (sheet) in a single Excel file using the writexl or openxlsx package in R. Here’s a programmatic approach using writexl for simplicity:

library(ggplot2) library(dplyr) library(writexl)

data("diamonds")
diamonds_split <- split(diamonds, diamonds$cut)
write_xlsx(diamonds_split, path = "diamonds_by_cut.xlsx")

split(): Splits the dataset into a list of data frames by cut. The names of the list (Ideal, Premium, etc.) become the sheet names automatically. write_xlsx(): Takes the list and writes each element to a separate sheet in a single Excel file.

Each sheet will correspond to a different diamond cut (Ideal, Premium, Good, Very Good, Fair). If you need more customization (like formatting, adding formulas, etc.), the openxlsx package would be ideal, but for straightforward tabbed exports, writexl is quick and clean.