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

From: Martin Maechler <maechler_at_stat.math.ethz.ch>
Date: Tue 04 Jul 2006 - 15:40:31 GMT

>>>>> "MM" == Martin Maechler <maechler@stat.math.ethz.ch> >>>>> on Fri, 30 Jun 2006 17:09:32 +0200 writes:

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

     PD> "BXC (Bendix Carstensen)" <bxc@steno.dk> writes:

     BXC> The function addmargins() adds margins to a table, but returns a matrix.
     BXC> But even after converted to a table the print.zero="." option of
     BXC> print.table() does not work:
     BXC> 
     BXC> > x <- sample( 1:7, 20, replace=T )
     BXC> > y <- sample( 1:7, 20, replace=T )
     BXC> > tt <- table( x, y )
     BXC> > tx <- as.table( addmargins( table( x, y ) ) )
     BXC> > print( tt, zero.print="." )
     BXC>    y
     BXC> x   1 2 3 4 5 6 7
     BXC>   1 1 2 2 . . 1 .
     BXC>   2 1 . . 1 . . .
     BXC>   3 . . . . . . 2
     BXC>   4 1 . . . . 1 .
     BXC>   5 1 . 1 . . 1 .
     BXC>   6 . 1 . 1 . . .
     BXC>   7 . . 1 . 1 1 .
     BXC> > print( tx, zero.print="." )
     BXC>      y
     BXC> x      1  2  3  4  5  6  7 Sum
     BXC>   1    1  2  2  0  0  1  0   6
     BXC>   2    1  0  0  1  0  0  0   2
     BXC>   3    0  0  0  0  0  0  2   2
     BXC>   4    1  0  0  0  0  1  0   2
     BXC>   5    1  0  1  0  0  1  0   3
     BXC>   6    0  1  0  1  0  0  0   2
     BXC>   7    0  0  1  0  1  1  0   3
     BXC>   Sum  4  3  4  2  1  4  2  20
     BXC> 
     BXC> Is this a facility of print.table?
     BXC> The attributes() of tt and tx have identical stucture. 

     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.

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

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

    MM> {and then for efficiency swap the *order* of the tests inside that if(.)}
    MM> which I think would be a bit more convenient and still ok (*)
    MM> here.

    MM> Martin Maechler, ETH Zurich

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

In R-devel,

Bendix' original example can now be slightly shortened *and* works as desired (in R-devel, aka 2.4.0 -- to-be):

> set.seed(1)
> x <- sample( 1:7, 20, replace=TRUE)
> y <- sample( 1:7, 20, replace=TRUE)
> tx <- addmargins( table(x, y) )
> print(tx, zero.print = ".")

     y
x      1  2  3  4  5  6  7 Sum
  1    .  .  1  .  .  .  .   1
  2    .  1  .  1  1  .  1   4
  3    .  2  .  .  .  1  .   3
  4    .  .  .  .  1  .  .   1
  5    .  .  1  1  1  .  1   4

  6 . . 1 . . 2 . 3
  7 3 . 1 . . . . 4
  Sum 3 3 4 2 3 3 2 20
>

Hoping, this will be useful..
Martin Maechler, ETH Zurich



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Wed Jul 05 01:49:34 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 06 Jul 2006 - 10:24:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.