How to suppress zero printing in a table (zero.print="" not working)
Asked Answered
S

2

9

I have diagonal matrices with NAs and zeros I want to hide.

na.print = "" is working fine, but zero.print = "." seems to treating 0.00 as != 0 ?

Here's a runnable example with print out so you can see what I mean:

x <- matrix(c(0.01, NA, NA, NA, 0.00, 0.00, NA, NA, 0.00, 0.00, -0.01, NA, 0.00, 0.00, 0.00, 0.00), nrow=4, byrow=TRUE)
x
         [,1] [,2]  [,3] [,4]
    [1,] 0.01   NA    NA   NA
    [2,] 0.00    0    NA   NA
    [3,] 0.00    0 -0.01   NA
    [4,] 0.00    0  0.00    0

print.table(x, na.print="", zero.print=".")
         [,1]  [,2]  [,3]  [,4] 
    [1,]  0.01                  
    [2,]  0.00  0.00            
    [3,]  0.00  0.00 -0.01      
    [4,]  0.00  0.00  0.00  0.00

Following the helpful answers below (thanks guys!), and based on the explicit choice in print.table to not zero.print items where any item in the table fails (x == round(x)), here's a version which works with floating.point. I wrote it for a dataframe printing task, but it works with matrices.

print.dataframe <- function (x, digits = getOption("digits"), quote = FALSE, na.print = "", zero.print = "0", justify = "none", ...){
    xx <- format(x, digits = digits, justify = justify)
    if (any(ina <- is.na(x))) 
        xx[ina] <- na.print
    i0 <- !ina & x == 0
    if (zero.print != "0" && any(i0)) 
        xx[i0] <- zero.print
    if (is.numeric(x) || is.complex(x)){
        print(xx, quote = quote, right = TRUE, ...)
    }else{
        print(xx, quote = quote, ...)   
    }
    invisible(x)
}

print.dataframe(bob, zero.print = ".", justify="left")
Steamtight answered 31/7, 2012 at 12:7 Comment(1)
Since table objects implicitly assumes that all elements are integers, print.table will not work for your data.Prototype
A
4

Here is a workaround:

> print.table(local({x[x==0] <- NA; x}))
     [,1]  [,2] [,3]  [,4]
[1,]  0.01                
[2,]                      
[3,]            -0.01     
[4,]                      

In the case that you need different expression for NA and zero, then,

> print(ifelse(is.na(x), "", ifelse(x == 0, ".", x)), quote = FALSE)
     [,1] [,2] [,3]  [,4]
[1,] 0.01                
[2,] .    .              
[3,] .    .    -0.01     
[4,] .    .    .     .  
Artimas answered 31/7, 2012 at 12:52 Comment(3)
Thank kohske. Looks like it is a bug then. PS: That was my workaround, but I'd rather have "." than blank.. ;-(Steamtight
@Steamtight This is most definitely not a bug. You are using the method for print.table to print a matrix - this may yield unexpected results.Prototype
Thanks @Andrie: Maybe table should down-cast everything to integers then, if it stores on only integers. x = as.table(matrix(seq(0,1,.1))) class(x) # table despite no integers print.table(x) Really, I think the request, then, becomes that the zero and NA handling facilities get added to the print generic, or to dataframe and matrix handling sub-classesSteamtight
P
3

Per Andrie's input and suggestion this is not a bug in the code with print.table. It's a problem with supplying a matrix to print.table which expects a table. As Andrie points out all bets are off with the expected output. If you add one additional line of code to print.table (seen below) it will work for you (note I called this printmatrix).

printmatrix <- 
function (x, digits = getOption("digits"), quote = FALSE, na.print = "", 
    zero.print = "0", justify = "none", ...) 
{
    xx <- format(unclass(x), digits = digits, justify = justify)
    if (any(ina <- is.na(x))) 
        xx[ina] <- na.print
    if (zero.print != "0" && any(i0 <- !ina & x == 0) && all(x == 
        round(x))) 
        xx[i0] <- sub("0", zero.print, xx[i0])
    xx[x == 0] <- zero.print                          #the line I added
    if (is.numeric(x) || is.complex(x)) 
        print(xx, quote = quote, right = TRUE, ...)
    else print(xx, quote = quote, ...)
    invisible(x)
}

printmatrix(x, zero.print = ".")
Pignut answered 31/7, 2012 at 13:28 Comment(4)
I see Andrie's comments above so this is not a bug.Pignut
I'm pretty sure it's not a bug. You are trying to print a matrix using the method for a table - any results you get will be unexpected. I suggest you modify your answer.Prototype
@Tyler Rinker Thanks Tyler: With " && all(x == round(x)))", they're explicitly not reformatting zeroes unless all values are integers portion... I don't see the purpose of that: Working with floats, but disabling the functionality to view zeros in a visually helpful way exactly when it is most useful (in a field of decimal values)Steamtight
@Tim Andrie's point is that if it's a table being passed to print.table it will be all integers.Pignut

© 2022 - 2024 — McMap. All rights reserved.