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

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Jun 29 12:18:13 CEST 2006


"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



More information about the R-help mailing list