问题
If I print a matrix, it is shown with row and column indices in the console. E.g.
> print(diag(3))
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
How can I suppress the column and row indices? I.e. something like this:
> print(diag(3), indices=FALSE)
1 0 0
0 1 0
0 0 1
I can see that the cwhmisc
package should contain a printM
function to do this according to the documentation but it is not there when I load cwhmisc. Also, this seems like something you should be able to to in base R.
回答1:
The function prmatrix in the base
package could work for this, it can take the arguments collab
and rowlab
:
prmatrix(diag(3), rowlab=rep("",3), collab=rep("",3))
1 0 0
0 1 0
0 0 1
回答2:
Another solution with function write.table
write.table(diag(3), row.names=F, col.names=F)
You can make it prettier by separating the columns with a tabulation
write.table(matrix(sample(1000,9),3,3), row.names=F, col.names=F, sep="\t")
来源:https://stackoverflow.com/questions/26906557/print-a-matrix-without-row-and-column-indices