Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid writing a row.names column when saving a data.frame using the xlsx package

Tags:

dataframe

r

xlsx

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?

like image 543
Davi Moreira Avatar asked Aug 24 '12 22:08

Davi Moreira


People also ask

How do I remove a column and row name in R?

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.

What does row names false do in R?

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.

How do I hide index in Excel?

1 Answer. You need to add parameter 'index=False' to function to_excel() to remove index column.


1 Answers

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.

like image 121
Andy Avatar answered Sep 22 '22 02:09

Andy