Cómo suprimir la impresión cero en una tabla (zero.print="" no funciona)

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")

preguntado el 31 de julio de 12 a las 13:07

Como table objects implicitly assumes that all elements are integers, print.table will not work for your data. -

2 Respuestas

Aquí hay una solución alternativa:

> 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,] .    .    .     .  

Respondido 31 Jul 12, 14:07

Thank kohske. Looks like it is a bug then. PS: That was my workaround, but I'd rather have "." than blank.. ;-( - Tim

@tim This is most definitely not a bug. You are using the method for print.table to print a matrix - this may yield unexpected results. - Andrie

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-classes - Tim

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 que espera un 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 = ".")

Respondido 31 Jul 12, 16:07

I see Andrie's comments above so this is not a bug. - Tyler Rinker

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. - Andrie

@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) - Tim

@Tim Andrie's point is that if it's a table being passed to print.table it will be all integers. - Tyler Rinker

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.