I have a data frame like this one below and I really want to remove the row names when I export it to a excel file using the xlsx package.
bd <- data.frame(id = 1:200, A = c(rep("One", 100), rep("Two", 100)),
B = c(rep(1,50), rep(0, 50), rep(1, 50), rep(0, 50)))
I have already tried to use the command below, but it keep them in the first column of the excel file.
bd <- data.frame(id = 1:200, A = c(rep("One", 100), rep("Two", 100)),
B = c(rep(1,50), rep(0, 50), rep(1, 50), rep(0, 50)), row.names=NULL)
Is there any way to do this?
Data Visualization using R Programming To remove the row names or column names from a matrix, we just need to set them to NULL, in this way all the names will be nullified.
The default (is back compatible), FALSE , will signal an error, where NA will “automatic” row names and TRUE will call make. names(value, unique=TRUE) for constructing valid names. an object to be coerced to character unless an integer vector.
1 Answer. You need to add parameter 'index=False' to function to_excel() to remove index column.
Set the rownames to NULL
to remove them:
rownames(bd) <- NULL
Also, from xlsx documentation:
write.xlsx(x, file, sheetName="Sheet1",
col.names=TRUE, row.names=TRUE, append=FALSE)
Set row.names to FALSE
to avoid the first column being row names.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With