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.
An example dataset (which is always a good idea to use in your question):
And now to produce something useful along the lines of the example table you posted:
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.