[R] Tables Package Grouping Factors

Duncan Murdoch murdoch.duncan at gmail.com
Sat Nov 9 20:30:10 CET 2013


On 13-11-09 1:23 PM, Jeff Newmiller wrote:
> Visually, the elimination of duplicates in hierarchical tables in the
> tabular function from the tables package is very nice. I would like to do
> the same thing with non-crossed factors, but am perhaps missing some
> conceptual element of how this package is used. The following code
> illustrates my goal (I hope):
>
> library(tables)
> sampledf <- data.frame( Sex=rep(c("M","F"),each=6)
>              , Name=rep(c("John","Joe","Mark","Alice","Beth","Jane"),each=2)
>              , When=rep(c("Before","After"),times=6)
>              , Weight=c(180,190,190,180,200,200,140,145,150,140,135,135)
>              )
> sampledf$SexName <- factor( paste( sampledf$Sex, sampledf$Name ) )
>
> # logically, this is the layout
> tabular( Name ~ Heading()* When * Weight * Heading()*identity,
> data=sampledf )
>
> # but I want to augment the Name with the Sex but visually group the
> # Sex like
> #   tabular( Sex*Name ~ Heading()*When * Weight * Heading()*identity,
> data=sampledf )
> # would except that there really is no crossing between sexes.
> tabular( SexName ~ Heading()*When * Weight * Heading()*identity,
> data=sampledf )
> # this repeats the Sex category excessively.

I forgot, there's a simpler way to do this.  Build the full table with 
the junk values, then take a subset:

full <- tabular( Sex*Name ~ Heading()*When * Weight * 
Heading()*identity, data=sampledf )

full[c(1:3, 10:12), ]

Figuring out which rows you want to keep can be a little tricky, but 
doing something like this might be good:

counts <- tabular( Sex*Name ~ 1, data=sampledf )
full[ as.logical(counts), ]

Duncan Murdoch



More information about the R-help mailing list