From murdoch at stats.uwo.ca  Sun Jun  4 03:14:47 2006
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Sat, 03 Jun 2006 21:14:47 -0400
Subject: [Rd] head() for tables
In-Reply-To: <44822EB7.3030306@ufl.edu>
References: <44822EB7.3030306@ufl.edu>
Message-ID: <44823407.6090408@stats.uwo.ca>

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



[Rd] head() for tables
Duncan Murdoch