Skip to content

Printing zero as dot

3 messages · Giovanni Petris, Sundar Dorai-Raj

#
I'm pretty sure I've seen some examples of a function printing zero
entries in a matrix as dots, but I'm not able to find it now...
Any suggestions...? Thanks in advance. (Of course, I might have dreamt
of such a function...) 

Best,
Giovanni
#
Giovanni Petris wrote:
I just so have something that may help:

print.matrix2 <- function(x, zero = ".", ...) {
   zeros <- which(x == 0, arr.ind = TRUE)
   x[zeros] <- zero
   print(x, quote = FALSE, right = TRUE, ...)
   invisible()
}

 > print.matrix2(diag(5))
      [,1] [,2] [,3] [,4] [,5]
[1,]    1    .    .    .    .
[2,]    .    1    .    .    .
[3,]    .    .    1    .    .
[4,]    .    .    .    1    .
[5,]    .    .    .    .    1
 >

Sundar
#
Many thanks to J.R. Lockwood and to Sundar Dorai-Raj for sending me
their suggestions. I ended up using Sundar's function blended with a
flavour of `zapsmall':

print.matrix2 <- function(x, zero = ".", digits=getOption("digits"), ...) {
    if (all(ina <- is.na(x))) 
        return(x)
    if (length(digits) == 0) 
        stop("invalid digits")
    mx <- max(abs(x[!ina]))
    x <- round(x, digits = if (mx > 0) 
               max(0, digits - log10(mx))
    else digits)
    zeros <- which(x == 0, arr.ind = TRUE)
    x[zeros] <- zero
    print(x, quote = FALSE, right = TRUE, ...)
    invisible()
}

Have a good weekend,
Giovanni