Hi,
Testing the examples added by Henrique to help of colnames and
rownames functions, I get different outputs for the M matrix:
? matrix M = {1,2;2,1;4,1}
matrix M = {1,2;2,1;4,1}
Matriz M gerada
? rownames(M, "Row1 Row2 Row3")
rownames(M, "Row1 Row2 Row3")
? print M
print M
M (3 x 2)
Row1 1 2
Row2 2 1
Row3 4 1
? colnames(M, "Col1 Col2")
colnames(M, "Col1 Col2")
? print M
print M
M (3 x 2)
Col1 Col2
Row1 1,0000 2,0000
Row2 2,0000 1,0000
Row3 4,0000 1,0000
?
---
This is not a big problem but maybe we should have some control on the
matrix elements numeric types (or at least consistency ;)). See ahead
that is the colnames setting the numbers to float.
In the following experimentation with printf, there are missing
separators within the matrix elements (output is using decimal commas
since is the PT version):
? matrix M = {1,2;2,1;4,1}
matrix M = {1,2;2,1;4,1}
Matriz M gerada
? printf "%d", M
12
21
41
? printf "%0.3g", M
12
21
41
? printf "%0.3f", M
1,0002,000
2,0001,000
4,0001,000
?
----
Here I add first the column names:
? colnames(M, "Col1 Col2")
colnames(M, "Col1 Col2")
? print M
print M
M (3 x 2)
Col1 Col2
1,0000 2,0000
2,0000 1,0000
4,0000 1,0000
? rownames(M, "Row1 Row2 Row3")
rownames(M, "Row1 Row2 Row3")
? print M
print M
M (3 x 2)
Col1 Col2
Row1 1,0000 2,0000
Row2 2,0000 1,0000
Row3 4,0000 1,0000
----
? matrix N = M*1.001
matrix N = M*1.001
Matriz N gerada
? print N
print N
N (3 x 2)
1,001 2,002
2,002 1,001
4,004 1,001
?
Needs a fix:
? printf "%1.4g", N
1,0012,002
2,0021,001
4,0041,001
?
Note that did not consider the requested 4 decimals.
----
? rownames(N, "Row1 Row2 Row3")
rownames(N, "Row1 Row2 Row3")
? print N
print N
N (3 x 2)
Row1 1,001 2,002
Row2 2,002 1,001
Row3 4,004 1,001
? colnames(N, "Col1 Col2")
colnames(N, "Col1 Col2")
? print N
print N
N (3 x 2)
Col1 Col2
Row1 1,0010 2,0020
Row2 2,0020 1,0010
Row3 4,0040 1,0010
?
It seems that colnames is setting 4 decimals by default :).
---
It seems that "%f" is OK:
? printf "%0.04g", N
Row1 1,0012,002
Row2 2,0021,001
Row3 4,0041,001
? printf "%0.04f", N
Row1 1,00102,0020
Row2 2,00201,0010
Row3 4,00401,0010
? printf "%0.05f", N
Row1 1,001002,00200
Row2 2,002001,00100
Row3 4,004001,00100
----
How about the printf when columm names are set?
? printf "%g", N
Col1 Col2
Row1 1,0012,002
Row2 2,0021,001
Row3 4,0041,001
? printf "%d", N
Col1Col2
Row1 12
Row2 21
Row3 41
?
---
Summary of fixes :
colnames function should leave numeric type untouched (like rownames does)
printf should use the same column separation as used by print
revise the format "%g" modifier in printf when argument is matrix
---
Bye,
Hélio