Skip to content
Prev 13327 / 398502 Next

Bug in ftable?? (Was: Two-way tables of data, etc)

"BXC (Bendix Carstensen)" <bxc at novonordisk.com> writes:

[tp<- tapply( breaks, list( wool, tension, variant ), mean )]
Um, I don't think so.... ;-)
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:
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:
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