Skip to content
Prev 332910 / 398503 Next

Tables Package Grouping Factors

On 13-11-09 1:23 PM, Jeff Newmiller wrote:
I don't think it's easy to get what you want.  The basic assumption is 
that factors are crossed.

One hack that would get you what you want in this case is to make up a 
new variable representing person within sex (running from 1 to 3), then 
treating the Name as a statistic.  Of course, this won't work if you 
don't have equal numbers of each sex.

A better solution is more cumbersome, and only works in LaTeX (and maybe 
HTML).  Draw two tables, first for the female subset, then for the male 
subset.  Put out the headers only on the first one and the footer only 
on the second, and it will be typeset as one big table.
You'll have to fight with the fact that the factors Sex and Name 
remember their levels whether they are present or not, but it should 
work.  For example,

sampledf$Sex <- as.character(sampledf$Sex)
sampledf$Name <- as.character(sampledf$Name)
females <- subset(sampledf, Sex == "F")
males <- subset(sampledf, Sex == "M")

latex( tabular( Factor(Sex)*Factor(Name) ~ Heading()*When * Weight * 
Heading()*identity, data=females),
options = list(doFooter=FALSE, doEnd=FALSE) )

latex( tabular( Factor(Sex)*Factor(Name) ~ Heading()*When * Weight * 
Heading()*identity, data=males),
options = list(doBegin=FALSE, doHeader=FALSE) )

It would probably make sense to support nested factor notation using 
%in% to make this easier, but currently tables doesn't do that.

Duncan Murdoch