Reputation: 39
I am working on a function to do my own output from some linear models and i am wanting to make a matrix of output i am trying to get a matrix with different decimal rules. for example if i have:
structure(c(1, 2, 3.45, 5.67), .Dim = c(4L, 1L), .Dimnames = list(c("A", "B", "C", "D"), NULL))'
is it possible to make rows 1 and 2 just show up as integers and the decimals stay in the last two rows? i know i could make two matrices and use rbind() but i want to keep the alignment the same so the columns line up well.
Upvotes: 1
Views: 627
Reputation: 81693
It is very easy if you transform your numeric matrix into a character matrix before printing:
print("[<-"(mat, as.character(mat)), quote = FALSE)
This displays:
[,1]
A 1
B 2
C 3.45
D 5.67
This above command is similar to:
mat[] <- as.character(mat)
print(mat, quote = FALSE)
but keeps mat
unchanged.
Upvotes: 3
Reputation: 59355
Output in R is formatted using the print(...)
function; use the digits= parameter to control the number of significant digits in the output.
So, if your matrix above is M
:
print(M[1:2,],digits=0)
# A B
# 1 2
print(M[3:4,],digits=3)
# C D
# 3.45 5.67
Upvotes: 2