I am trying to replicate a table:
which shows counts of people who are grouped on five binary variables. I can load the data and get the counts to display with lousy labels like this:
Drugs2 <- read.table("http://users.stat.ufl.edu/~aa/cat/data/Substance2.dat",
header = TRUE, stringsAsFactors = TRUE)
library(dplyr)
`Table 7.5` <- bind_cols(`Alcohol Use` = c("Yes", "", "No", ""),
`Cigarette Use` = rep(c("Yes", "No"),2),
matrix(Drugs2$count, ncol = 8,byrow = T,
dimnames = list(NULL,
c("Y_F_W", "N_F_W", "Y_M_W", "N_M_W", "Y_F_O", "N_F_O", "Y_M_O", "N_M_O"))))
knitr::kable(`Table 7.5`)
I see that the gtsummary
package can add one level of column labels with the modify_spanning_header()
function but I don't see how to nest spanning headers. Does anybody know how I can add nested headers with gtsummary
?
Can anybody suggest how to produce a publication quality version of this table using gtsummary
or some other package?
You're correct that gtsummary cannot add multiple spanning headers (it's written that way to ensure we support output to multiple formats).
I would try flextable to print this table. They have functions for adding multiple headers row, and the ability to merge cells (both vertically and horizontally). https://davidgohel.github.io/flextable/