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

空扰寡人 提交于 2019-12-20 10:57:54

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/12117629/how-to-avoid-writing-a-row-names-column-when-saving-a-data-frame-using-the-xlsx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!