BXC (Bendix Carstensen)
2001-Sep-05 08:50 UTC
[R] Bug in ftable?? (Was: Two-way tables of data, etc)
Further to the discussion between Murray Jorgensen and Brian Ripley, it seems to me better to choose tabulations that will not come and bite you. Suppose your data are sligtly irregular, e.g. (for the sake of the argument): data( warpbreaks ) warpbreaks$variant <- rep( 1:5, len=54 ) attach( warpbreaks ) tb <- table( wool, tension, variant ) tb # in this case you would like to see: tp <- tapply( breaks, list( wool, tension, variant ), mean ) tp # and hardly: ftable( xtabs( breaks ~ wool + tension + variant ) ) # It would be nice to flatten the tabulations, so I tried: ftable( tb ) 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) # 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 ? Bendix Carstensen ---------------------- Bendix Carstensen Senior Statistician Steno Diabetes Centre Niels Steensens Vej 2 DK-2820 Gentofte Denmark tel: +45 44 43 87 38 mob: +45 28 25 87 38 fax: +45 44 43 73 13 bxc at novo.dk www.biostat.ku.dk/~bxc ---------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Peter Dalgaard BSA
2001-Sep-05 09:32 UTC
[R] 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 )]> 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
BXC (Bendix Carstensen)
2001-Sep-05 11:06 UTC
[R] Bug in ftable?? (Was: Two-way tables of data, etc)
Thanks to Peter Dalgaard for the helpful hints aboy dimnames. This will work: data( warpbreaks ) warpbreaks$variant <- rep( 1:5, len=54 ) attach( warpbreaks ) tb <- table( wool, tension, variant ) tb # in this case you would like to see: tp <- tapply( breaks, list( wool, tension, variant ), mean ) tp dimnames( tp ) <- dimnames( tb ) ftable( tp ) Slightly clumsy, but a workaround. Bendix C ---------------------- Bendix Carstensen Senior Statistician Steno Diabetes Centre Niels Steensens Vej 2 DK-2820 Gentofte Denmark tel: +45 44 43 87 38 mob: +45 28 25 87 38 fax: +45 44 43 73 13 bxc at novo.dk www.biostat.ku.dk/~bxc ----------------------> -----Original Message----- > From: Peter Dalgaard BSA [mailto:p.dalgaard at biostat.ku.dk] > Sent: 5. september 2001 11:33 > To: BXC (Bendix Carstensen) > Cc: Murray Jorgensen; 'Prof Brian D Ripley'; r-help at stat.math.ethz.ch > Subject: Re: [R] 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 )] > > > 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 -- Readhttp://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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Kurt Hornik
2001-Sep-06 08:49 UTC
[Rd] Re: [R] Bug in ftable?? (Was: Two-way tables of data, etc)
>>>>> 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.0I 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._