I have exported my dataframe into 3 different Excel workbooks based on their "Country" and I have another dataframe that have the same exact countries and I would like them to be exported to the current existing workbook in which I have created.
df = data.frame(Year = c(2018,2019,2020,2018,2019,2020,2018,2019,2020),
Country = c("Germany","Germany","Germany", "Japan", "Japan", "Japan", "Thailand", "Thailand", "Thailand"),
Count = c(17, 15, 60, 23, 25, 60, 50, 18, 31))
df2 = data.frame(Country = c("Germany","Germany","Germany", "Japan", "Japan", "Japan", "Thailand", "Thailand", "Thailand"),
Count = c("James", "Gordon", "Jackson", "Harrison", "Reid", "Ashen", "Lewis", "Maokai", "Federick"))
#Split dataframe according to their Countries
splitdf = split(df, df$Country)
#Write workbook
save_data <- function(df, name) {
wb <- createWorkbook()
addWorksheet(wb, name)
writeDataTable(wb, name, df, tableStyle = "TableStyleLight9")
saveWorkbook(wb, paste0(name, ".xlsx"), overwrite = TRUE)
}
#Print workbook
mapply(
save_data,
splitdf,
names(splitdf)
)
Is there a way for me to append df2 to a new sheet based on it's unique Countries to the existing Workbooks that I have created without overwriting the original file?