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

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Sep 5 11:32:43 CEST 2001


"BXC (Bendix Carstensen)" <bxc at 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


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list