79446172

Date: 2025-02-17 17:32:09
Score: 1.5
Natty:
Report link

What about using openxlsx?

out

library(openxlsx)

wb <- createWorkbook() # Save the workbook
addWorksheet(wb, 1)

main_headers <- c("", "", rep("value", 10), rep("share", 10))  # Top row
sub_headers <- c("year", "type", rep(LETTERS[1:10], 2))  # Bottom row

# Write headers
writeData(wb, 1,  matrix(main_headers,ncol=length(main_headers)),
          startCol = 1,
          startRow = 1,
          colNames = FALSE)
writeData(wb, 1, 
          matrix(sub_headers,ncol=length(sub_headers)), 
          startRow = 2, 
          startCol = 1,
          colNames = FALSE)

mergeCells(wb, 1, cols = 3:12, rows = 1)  # Merge "value" columns
mergeCells(wb, 1, cols = 13:22, rows = 1) # Merge "share" columns

writeData(wb, 1, my_table, startRow = 3, colNames = FALSE) # Define headers

saveWorkbook(wb, "Multiheaded_Table.xlsx", overwrite = TRUE) # Save the workbook
Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Starts with a question (0.5): What
  • High reputation (-1):
Posted by: dog