Q:

How do you create a nested table in R?

I know there is a way to easily do this using a package... I found the package a couple of months ago and did it then. However, I didn't write down the link at the time, and now I can't find the solution again for the life of me!
My question is: how do you create a nested table in R? For example, I have a data table of individuals. Every row is a different person. Every column contains a variable such as age, gender, height, etc.
I know that using the table command I can do something like this: table(data$gender, data$ageCategory).
This would make a 2 x 2 table with the counts inside.
However, I would like to make a nested table, analogous to the one here (ignore the final column with the totals): http://psycnet.apa.org/journals/ror/32/2/images/ror_32_2_199_tbl1a.gif
I.e., I would like to make a table with gender as the columns, then with the rows as age category, and within each row, additional rows with height category.
I found a package that did this easily and now I can't find it again!
Thanks for anyone who can help. This is a somewhat silly question but I thought I might have more luck here. Perhaps "nested table" isn't really the proper name for this.

A:

An example dataset (which is always a good idea to use in your question):
dat = data.frame(value = runif(100),
                 age = round(runif(100, min = 9, max = 11)) , 
                 gender = sample(c("Male", "Female"), 100, replace = TRUE), 
                 school = sample(c("Public", "Private"), 100, replace = TRUE))

And now to produce something useful along the lines of the example table you posted:
library(plyr)
tab = count(dat, c("age", "gender", "school"))
# The contents of tab:
#    age gender  school freq
# 1    9 Female Private    6
# 2    9 Female  Public    6
# 3    9   Male Private    7
# 4    9   Male  Public    8
# 5   10 Female Private   11
# 6   10 Female  Public   13
# 7   10   Male Private   10
# 8   10   Male  Public   14
# 9   11 Female Private    3
# 10  11 Female  Public   11
# 11  11   Male Private    3
# 12  11   Male  Public    8

And to produce a table for latex, take a look at the xtable package. For Word etc it is probably easiest to perform some manual operations on the output of count. 

