I've a data frame Matrix that I want to output to Excel with the following code:
writeData(wb_Matrix, list(Worksheet), Matrix, startCol = 2, startRow = 5)
Matrix looks like below when printed in R (column names dropped using colnames(Matrix) <- NULL)
c 86 2 2 4 6
c1 57 4 4 4 30
c2 33 17 0 17 33
c3 22 0 7 33 37
c4 4 0 0 0 96
In Excel, it becomes mangled like below, with 2 lines of header (one with V and the other X) added and data repeated in a single column after the last row.
V1 V2 V3 V4 V5
1 X1 X2 X3 X4 X5
2 86 2 2 4 6
3 57 4 4 4 30
4 33 17 0 17 33
5 22 0 7 33 37
4 0 0 0 96
NA NA NA NA NA
4
4
4
30
33
17
0
`
I've searched the web and can't find any post with similar problem.
I don't want the V and X headers and want to preserve the 5x5 format.
Any help is appreciated.
vkatti
November 28, 2022, 11:47am
2
add argument colNames=FALSE
in the writeData
statement.
1 Like
Thanks. I made change as suggested:
writeData(wb_Matrix, list(Worksheet), Matrix, startCol = 2, startRow = 5, colNames = FALSE, rowNames = FALSE)
but it doesn't work
V1 V2 V3 V4 V5
1 X1 X2 X3 X4 X5
2 93 4 2 1 1
3 71 16 7 3 3
4 51 15 19 7 8
5 35 11 13 22 19
4 1 1 1 93
NA NA NA NA NA
16
7
3
type or paste code here
FJCC
November 29, 2022, 2:41am
4
This works for me to write only the matrix values with no column headers to the Excel file.
library(openxlsx)
Matrix <- matrix(c(93,71,51,35,4,
4,16,15,11,1,
2,7,19,13,1,
1,3,7,22,1,
1,3,8,19,93),nrow = 5)
Matrix
wb <- createWorkbook()
addWorksheet(wb,"wb_Matrix")
writeData(wb, "wb_Matrix", Matrix, startCol = 2, startRow = 5,colNames = FALSE)
saveWorkbook(wb, file = "forumMatrix.xlsx")
Thanks a lot. It works. My earlier code was to load an already created Excel workbook which seems problematic.
system
Closed
January 10, 2023, 7:58am
6
This topic was automatically closed 42 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.