tableone icon indicating copy to clipboard operation
tableone copied to clipboard

How to create a table with hierarchical groups?

Open wasmetqall opened this issue 7 years ago • 1 comments

Many thanks to your package! I want to create a table like below: image

Say I have data like this:

Gram Species Count
pos A 1
pos B 1
neg C 1
neg D 1
neg D 1

How to output first table using tableone? THANK YOU!

wasmetqall avatar Feb 23 '19 15:02 wasmetqall

Short answer: It's not a good fit.

Long answer: Use interaction(Gram,Species) to create a single variable denoting both. Use Counts as weights in the svydesign(). Use svyCreateTableOne() (https://github.com/kaz-yos/tableone/blob/master/R/svyCreateTableOne.R). print(svyTableOneObject, format = "f") to get frequencies version. print(svyTableOneObject, format = "p") to get the percentage version. Capture both tables as tabf and tabp (these are matrices). Justapose them using cbind(). Subtotal and Total has to be done if you subset the matrices and sum up and add back a new row.

Alternative answer: Just use dplyr. Augment your dataset with Total = sum(Count). group_by(Gram, Species) %>% summary(n = sum(Count)). Now you can divide by the total to get %. There must be something on Totals and Subtotals.

kaz-yos avatar Feb 23 '19 15:02 kaz-yos