[Rd] [R] zero.print in print.table after adding margins

Martin Maechler maechler at stat.math.ethz.ch
Fri Jun 30 17:09:32 CEST 2006


>>>>> "PD" == Peter Dalgaard <p.dalgaard at biostat.ku.dk>
>>>>>     on 29 Jun 2006 12:18:13 +0200 writes:

 PD> "BXC (Bendix Carstensen)" <bxc at steno.dk> writes:
 PD> 
 PD> > The function addmargins() adds margins to a table, but returns a matrix.
 PD> > But even after converted to a table the print.zero="." option of
 PD> > print.table() does not work:
 PD> > 
 PD> > > x <- sample( 1:7, 20, replace=T )
 PD> > > y <- sample( 1:7, 20, replace=T )
 PD> > > tt <- table( x, y )
 PD> > > tx <- as.table( addmargins( table( x, y ) ) )
 PD> > > print( tt, zero.print="." )
 PD> >    y
 PD> > x   1 2 3 4 5 6 7
 PD> >   1 1 2 2 . . 1 .
 PD> >   2 1 . . 1 . . .
 PD> >   3 . . . . . . 2
 PD> >   4 1 . . . . 1 .
 PD> >   5 1 . 1 . . 1 .
 PD> >   6 . 1 . 1 . . .
 PD> >   7 . . 1 . 1 1 .
 PD> > > print( tx, zero.print="." )
 PD> >      y
 PD> > x      1  2  3  4  5  6  7 Sum
 PD> >   1    1  2  2  0  0  1  0   6
 PD> >   2    1  0  0  1  0  0  0   2
 PD> >   3    0  0  0  0  0  0  2   2
 PD> >   4    1  0  0  0  0  1  0   2
 PD> >   5    1  0  1  0  0  1  0   3
 PD> >   6    0  1  0  1  0  0  0   2
 PD> >   7    0  0  1  0  1  1  0   3
 PD> >   Sum  4  3  4  2  1  4  2  20
 PD> > 
 PD> > Is this a facility of print.table?
 PD> > The attributes() of tt and tx have identical stucture. 
 PD> 
 PD> It appears to be intentional.
 PD> 
 PD> print.table has
 PD> 
 PD>     if (is.integer(x) && zero.print != "0" && any(i0 <- !ina &
 PD>         x == 0))
 PD>         xx[i0] <- sub("0", zero.print, xx[i0])
 PD> 
 PD> and of course,
 PD> 
 PD> > storage.mode(tx)
 PD> [1] "double"
 PD> > storage.mode(tt)
 PD> [1] "integer"
 PD> 
 PD> The reason could be that it is not entirely clear what to expect for
 PD> values that are zero up to round-off.
 PD> 
 PD> storage.mode(tx) <- "integer" fixes things up.

On the other hand, I'm pretty sure I was the one who added
'zero.print' and I don't oppose at all to change

   is.integer(x)   to   all(x == round(x))

{and then for efficiency swap the *order* of the tests inside that if(.)}

which I think would be a bit more convenient and still ok (*)
here.

Martin Maechler, ETH Zurich

(*) yes, one could then construct artificial cases where the
    if(.) test would ``conceptually'' be wrong, but I think that
    would not matter for all practical cases.



More information about the R-devel mailing list