[Rd] head() for tables

Duncan Murdoch murdoch at stats.uwo.ca
Sun Jun 4 03:14:47 CEST 2006


On 6/3/2006 8:52 PM, Ben Bolker wrote:
>    head() doesn't do anything particularly
> useful for tables ...
> 
>    For example:
> 
> z <- sample(1:20,size=1000,replace=TRUE)
> z2 <- sample(1:20,size=1000,replace=TRUE)
> t1 <- table(z,z2)
> head(t1)
> 
>   as.matrix() doesn't help ...
> head(as.matrix(t1))
> 
>    this does ...
> 
> class(t1) <- "matrix"
> 
>   so does
> 
>   head.table <- utils:::head.matrix
> 
>    is it worth making this duplication in the code?

This seems to be a deeper problem.  as.matrix(t1) still returns an 
object of class "table", so head(as.matrix(t1)) dispatches to 
head.default, rather than to head.matrix.  You can force the latter by

utils:::head.matrix(t1)

and get reasonable output.

Perhaps as.matrix(x) should strip off the class attribute, or add a 
"matrix" class attribute?  Or perhaps head.default should check for 
is.matrix?

Duncan Murdoch



More information about the R-devel mailing list