Print matrix without column names but kept alligned?

非 Y 不嫁゛ 提交于 2021-02-17 05:25:07

问题


I would like to print a matrix without column names and found this answer. However, this will result in the columns of the output not being aligned anymore, when the row names are kept and are of different length:

m <- matrix(LETTERS[1:12],nrow = 2, ncol = 6)
rownames(m) <- c("First Row", "Second Row")

Using print just ignores the col.names = FALSE argument (why?):

print(m, col.names=FALSE, quote=FALSE)
>            [,1] [,2] [,3] [,4] [,5] [,6]
> First Row  A    C    E    G    I    K   
> Second Row B    D    F    H    J    L 

Using write.table as proposed removes the alignment:

write.table(format(m, justify="right"), col.names=FALSE, quote=FALSE)
> First Row A C E G I K
> Second Row B D F H J L

What can I do to keep row names and the alignment intact?


回答1:


If you really don't want matrix or table class, then you can use data frame, by using that you can easily hide column names and make data kept aligned.

>d = as.data.frame(m)
>colnames(d)=NULL
>d                   
 First Row  A C E G I K
 Second Row B D F H J L


来源:https://stackoverflow.com/questions/50760888/print-matrix-without-column-names-but-kept-alligned

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