Writing several data frames into 1 Excel spreadsheet

Hello. I need to write 2 data tables on the same list in one Excel spreadsheet. I run the following code:

write.xlsx(c(VNINDEXlogreturns_train,VNINDEX_predictions_total), "VNINDEX_predictions.xlsx",sheetName = "Sheet1")

I'd like to get 2 data tables, separated by 1 or several columns on the same spreadsheet. But after running this code I don't get the desired result.

How can I solve this problem?

Thank you for your effort.

I'm not sure what package you're using, but here's an option using the openxlsx package. We'll write the built-in mtcars and iris data frames.

library(openxlsx)

wb = createWorkbook()
sht = addWorksheet(wb, "my sheet")

start.cols = c(1, ncol(mtcars) + 2)

walk2(list(mtcars, iris), start.cols, 
      ~writeData(wb, sht, .x, startCol=.y))

saveWorkbook(wb, "test.xlsx")

@joels When I run your code, I get the following error:

Error in walk2(list(mtcars, iris), start.cols, ~writeData(wb, sht, .x,  : 
  could not find function "walk2"

What is walk2?

I use to exploit xlsx package.

purrr library within tidyverse package contains many useful map and walk functions (including walk2)

Thanks, after installing tidyverse and purrr code started working!