How to make a nested table in R?

56 views Asked by At

I have a dataframe as df with the following columns:

df <- data.frame(
years = c(2010, 2010, 2011, 2011), 
category = c("Category1", "Category2", "Category1", "Category2"), 
type = c("Type1", "Type2", "Type1", "Type2"), 
theme = c("Theme1", "Theme2", "Theme1", "Theme2"),
count = c(40, 50, 10, 70)
)

I would like to make a table like this in R:

                           Category1               Category2
                      2010    |   2011            2010 |  2011
Type1
  Theme1                x            x              x       x
Type2
  Theme2                x            x              x       x

Where x is the average count number per year per category per type and per theme.

Any ideas on how to do this?

1

There are 1 answers

0
jdtrulson On

This will not be exactly the same, but you could use the table1 package to get something similar to what you are looking for.

library(table1)

table1(~ count + type + theme | category * years, data = df, topclass = "Rtable1-grid Rtable1-shade Rtable1-times", overall = FALSE, render.continuous = "Mean")