问题
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