打印没有行和列索引的矩阵

时间:2021-10-31 21:45:15

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.

我可以看到cwhmisc包应该包含一个printM函数来根据文档执行此操作,但是当我加载cwhmisc时它不存在。此外,这似乎是你应该能够在基地R的东西。

2 个解决方案

#1


10  

The function prmatrix in the base package could work for this, it can take the arguments collab and rowlab:

基础包中的函数prmatrix可以为此工作,它可以采用参数collab和rowlab:

prmatrix(diag(3), rowlab=rep("",3), collab=rep("",3))

 1 0 0
 0 1 0
 0 0 1

#2


8  

Another solution with function write.table

另一个函数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")

#1


10  

The function prmatrix in the base package could work for this, it can take the arguments collab and rowlab:

基础包中的函数prmatrix可以为此工作,它可以采用参数collab和rowlab:

prmatrix(diag(3), rowlab=rep("",3), collab=rep("",3))

 1 0 0
 0 1 0
 0 0 1

#2


8  

Another solution with function write.table

另一个函数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")