Suppose I have the following dataframe in R and I am looking to split the dataframe into separate Excel sheets categorised by the Fruit column
+--------+-------+
| Fruit | Price |
+--------+-------+
| Apple | 12 |
| Apple | 14 |
| Apple | 15 |
| Orange | 2 |
| Orange | 4 |
| Orange | 6 |
| Pear | 3 |
| Pear | 6 |
| Pear | 9 |
+--------+-------+
After splitting the dataframe into 3 separate dataframes (Apple, Orange and Pear), I intend to export each dataframe into a separate Excel sheets (named Apple, Orange and Pear) but stored within the same Excel workbook Out.xlsx
. However, the below R code does not work. The output is an Excel workbook Out.xlsx
with only a single sheet, Pear, containing the Pear dataframe.
library(openxlsx)
df <- read_excel("Export excel test.xlsx")
output <- split(df, df$Fruit)
for (i in 1:length(output)){write.xlsx(x = output[i],
file = "Out.xlsx", sheetName = names(output[i]),append = TRUE)}
Would anyone be able to help on this? My actual dataframe has over 4 million rows, hence I need to split the dataframe into separate sheets to circumvent Excel's row limitations