[Rd] Re: [R] Bug in ftable?? (Was: Two-way tables of data, etc)

Kurt Hornik Kurt.Hornik@ci.tuwien.ac.at
Thu, 6 Sep 2001 10:49:02 +0200


>>>>> Peter Dalgaard BSA writes:

> "BXC (Bendix Carstensen)" <bxc@novonordisk.com> writes:
> [tp<- tapply( breaks, list( wool, tension, variant ), mean )]

>> tp <- as.table( tp )
>> ftable( tp )
>> 
>> But here i get:
>> 
>> > attr( tp, "class" ) <- "table"
>> > ftable( tp )
>> Error in cbind(...) : number of rows of matrices must match (see arg 2)

> Um, I don't think so.... ;-)

>> # But still they seem to have similar attributes (except for the naming)
>> attributes( tb )
>> attributes( tp )
>> 
>> Is this a bug or a facility of ftable ?

> It's a bug *somewhere*, not necessarily in ftable. The problem is
> either that the dimname-names are not added by tapply, that neither
> as.table or ftable add dummy names, or that write.ftable gags when
> names are absent. (Or put differently: Are either the "table" or
> "ftable" objects *required* to have named attributes??). 

> Actually, the fact that tapply doesn't generated named dimnames cannot
> be considered the cause since 
> (a) as.table should work on any matrix.
> (b) the second argument to tapply is not a named list in your case

> One  workaround is to add in the names:

>   ft <- ftable(tp)
>   names(attr(ft,"row.vars")) <- c("wool","tension")
>   names(attr(ft,"col.vars")) <- "variant"
>   ft

> another is to make sure that tapply gets the names right:

>> tp <- tapply(breaks, list(wool=wool,tension=tension,variant=variant), mean)
>> ftable(tp)
>              variant    1    2    3    4    5
> wool tension                                 
> A    L               39.0 40.5 40.0 46.0 70.0
>      M               28.0 29.5 26.5 12.0 18.0
>      H               19.5 22.0 10.0 39.5 24.5
> B    L               31.5 29.0 29.0 27.5 24.5
>      M               39.0 35.0 23.5 29.0 22.5
>      H               17.5 18.0 20.0 22.5 13.0

> or - lazy variation - abuse the fact that dataframes are lists and do
> have labeled columns:

>> tp <- tapply( breaks, data.frame(wool,tension,variant), mean )
>> ftable(tp)
>              variant    1    2    3    4    5
> wool tension                                 
> A    L               39.0 40.5 40.0 46.0 70.0
>      M               28.0 29.5 26.5 12.0 18.0
>      H               19.5 22.0 10.0 39.5 24.5
> B    L               31.5 29.0 29.0 27.5 24.5
>      M               39.0 35.0 23.5 29.0 22.5
>      H               17.5 18.0 20.0 22.5 13.0

I think we need to improve as.table.

table() is trying hard to produce variable names, and ftable() [and also
xtabs(), of course] assumes that we have them.

I would suggest enhancing as.table.default to provide dimname names in
addition to adding the "table" class attribute.

-k
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._