BXC (Bendix Carstensen)
2006-Jun-29 09:34 UTC
[R] zero.print in print.table after adding margins
The function addmargins() adds margins to a table, but returns a matrix. But even after converted to a table the print.zero="." option of print.table() does not work:> x <- sample( 1:7, 20, replace=T ) > y <- sample( 1:7, 20, replace=T ) > tt <- table( x, y ) > tx <- as.table( addmargins( table( x, y ) ) ) > print( tt, zero.print="." )y x 1 2 3 4 5 6 7 1 1 2 2 . . 1 . 2 1 . . 1 . . . 3 . . . . . . 2 4 1 . . . . 1 . 5 1 . 1 . . 1 . 6 . 1 . 1 . . . 7 . . 1 . 1 1 .> print( tx, zero.print="." )y x 1 2 3 4 5 6 7 Sum 1 1 2 2 0 0 1 0 6 2 1 0 0 1 0 0 0 2 3 0 0 0 0 0 0 2 2 4 1 0 0 0 0 1 0 2 5 1 0 1 0 0 1 0 3 6 0 1 0 1 0 0 0 2 7 0 0 1 0 1 1 0 3 Sum 4 3 4 2 1 4 2 20 Is this a facility of print.table? The attributes() of tt and tx have identical stucture. Best, Bendix ---------------------- Bendix Carstensen Senior Statistician Steno Diabetes Center Niels Steensens Vej 2 DK-2820 Gentofte Denmark tel: +45 44 43 87 38 mob: +45 30 75 87 38 fax: +45 44 43 07 06 bxc at steno.dk www.biostat.ku.dk/~bxc
"BXC (Bendix Carstensen)" <bxc at steno.dk> writes:> The function addmargins() adds margins to a table, but returns a matrix. > But even after converted to a table the print.zero="." option of > print.table() does not work: > > > x <- sample( 1:7, 20, replace=T ) > > y <- sample( 1:7, 20, replace=T ) > > tt <- table( x, y ) > > tx <- as.table( addmargins( table( x, y ) ) ) > > print( tt, zero.print="." ) > y > x 1 2 3 4 5 6 7 > 1 1 2 2 . . 1 . > 2 1 . . 1 . . . > 3 . . . . . . 2 > 4 1 . . . . 1 . > 5 1 . 1 . . 1 . > 6 . 1 . 1 . . . > 7 . . 1 . 1 1 . > > print( tx, zero.print="." ) > y > x 1 2 3 4 5 6 7 Sum > 1 1 2 2 0 0 1 0 6 > 2 1 0 0 1 0 0 0 2 > 3 0 0 0 0 0 0 2 2 > 4 1 0 0 0 0 1 0 2 > 5 1 0 1 0 0 1 0 3 > 6 0 1 0 1 0 0 0 2 > 7 0 0 1 0 1 1 0 3 > Sum 4 3 4 2 1 4 2 20 > > Is this a facility of print.table? > The attributes() of tt and tx have identical stucture.It appears to be intentional. print.table has if (is.integer(x) && zero.print != "0" && any(i0 <- !ina & x == 0)) xx[i0] <- sub("0", zero.print, xx[i0]) and of course,> storage.mode(tx)[1] "double"> storage.mode(tt)[1] "integer" The reason could be that it is not entirely clear what to expect for values that are zero up to round-off. storage.mode(tx) <- "integer" fixes things up. -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Martin Maechler
2006-Jun-30 15:09 UTC
[Rd] [R] zero.print in print.table after adding margins
>>>>> "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.