xarena
xarena

Reputation: 95

Append dataframes to existing excel file's new sheets in R

I have a excel file data.xlsx, which has two sheets Sheet1 and Sheet2, now I want to append df1 and df2 from R into data.xlsx with names Sheet3 and Sheet4, I have tried with:

library(xlsx)
write.xlsx(df1, "data.xlsx", sheetName = "Sheet3", row.names = FALSE)
write.xlsx(df2, "data.xlsx", sheetName = "Sheet4", row.names = FALSE)

But it overwrites the orginal file's Sheet1 and Sheet2, how could we solve this problem?

Thanks.

Upvotes: 0

Views: 1562

Answers (2)

xarena
xarena

Reputation: 95

This solution may works:

library(xlsx)
# Write the first dataset df1 in a workbook
write.xlsx(df1, file = 'data.xlsx', sheetName = 'Sheet3, append = TRUE)
# Add a second dataset df2 in a new worksheet
write.xlsx(df2, file = 'data.xlsx', sheetName = 'Sheet4', append = TRUE)

Upvotes: 1

Related Questions