# [R] Printing numerical matrices

Aleš Žiberna ales.ziberna at gmail.com
Tue Jan 17 16:06:40 CET 2006

```Dear useRs!

I athought this was a trival question, however I could not fin dan answer in
the help files for print, format or formatC. I would like to print a
numerical matrix so that all cells (all rows/colums) are printed:
a)	with the same number of decimal places (numbers after the decimal
separator)
b)	with the same number of numbers (regardless of  ".")
c)	with the same number of characters (including "." if present, with
option for ignoring or not the "-" sign)) - if the last character would be
".", it is ommited.

Let say I have a matrix M
[,1]        [,2]     [,3]       [,4] [,5]
[1,]    1 -0.87330578 14.72961  1.0885293  101
[2,]    2 -0.61406616 13.63638 -0.3124361  102
[3,]    3 -0.03322147 15.15509 -1.6021408  103
[4,]    4  1.58780140 15.00857 -0.1908761  104
[5,]    5  0.75340104 14.80913  0.3210328  105

Which you can get (at least similar) by:
M<- matrix(rnorm(25),ncol=5,nrow=5)
M[,1]<-1:5
M[,3]<-M[,3]+15
M[,5]<-101:105

If I select the number of 3, I want to get:
a)
[,1]   [,2]   [,3]   [,4]     [,5]
[1,]    1.000 -0.873 14.729  1.088  101.000
[2,]    2.000 -0.614 13.636 -0.312  102.000
[3,]    3.000 -0.033 15.155 -1.602  103.000
[4,]    4.000  1.588 15.009 -0.190  104.000
[5,]    5.000  0.753 14.809  0.321  105.000

b)
[,1]  [,2] [,3]  [,4]  [,5]
[1,]    1.00 -0.87 14.7  1.09  101
[2,]    2.00 -0.61 13.6 -0.31  102
[3,]    3.00 -0.03 15.2 -1.60  103
[4,]    4.00  1.58 15.0 -0.19  104
[5,]    5.00  0.75 14.8  0.32  105

c) (ignoring "-" sign)
[,1] [,2] [,3] [,4] [,5]
[1,]    1.0 -0.9   14  1.1  101
[2,]    2.0 -0.6   14 -0.3  102
[3,]    3.0 -0.0   15 -1.6  103
[4,]    4.0  1.6   15 -0.2  104
[5,]    5.0  0.8   15  0.3  105

c) (counting the  "-" sign)
[,1] [,2] [,3] [,4] [,5]
[1,]    1.0   -1   14  1.1  101
[2,]    2.0   -1   14   -0  102
[3,]    3.0   -0   15   -2  103
[4,]    4.0  1.6   15   -0  104
[5,]    5.0  0.8   15  0.3  105

I would appreciate a solution for any of those options.

Best regards,
Ales

```