Skip to contents

Multiple columns of Row Labels

It is not all that unusual for listings (and some tables) to have multiple row label columns. When this happens, it is often easier to avoid using gt’s out-of-the box stub functions/formatting. An example of a table like this is the “Summary of Number of Subjects by Site” from the CDISC pilot. Image of the Summary of Number of Subjects By Site table from the CDISC pilot

To make this table the values will be long with “Pooled Id” and “Site Id” in their own columns, as if they were group or label variables. We also will need a column for the parameters even though they are all the same.

data <- tribble(
  ~`Pooled Id`,  ~`Site Id`,
"701",  "701",
"703",  "703",
"704",  "704",
"705",  "705",
"708",  "708",
"709",  "709",
"710",  "710",
"713",  "713",
"716",  "716",
"718",  "718",
"900",  "702",
"900",  "706",
"900",  "707",
"900",  "711",
"900",  "714",
"900",  "715",
"900",  "717",
"Total", " ") %>%
  crossing(col1 = c("Placebo (N=86)",
                   "Xanomeline Low Dose (N=84)",
                   "Xanomeline High Dose (N=84)",
                   "Total (N=254)"),
           col2 = factor(c("ITT", "Eff", "Com"), levels = c("ITT", "Eff", "Com"))) %>%
  mutate(val = rpois(216, 15), # Here I am just faking the data for display purposes 
         param = "val")

Once we have the data in the standard ARD format we can make the tfrmt. What makes this tfrmt different is we won’t include group or label, and our two ID columns will be displayed as regular columns. This also means that all columns of the table, including the ID columns, can be ordered via the col_plan(). Because the col_plan() follows the conventions of select() we can’t specify the order of the highest level spanning columns and the lower level columns. But, tfrmt respects the order things are put in, which is why we used a factor for the populations.

tfrmt(
  param = "param",
  value = "val",
  column = vars(col1, col2),
  body_plan = body_plan(
    frmt_structure(group_val = ".default", label_val = ".default", frmt("XX"))
  ),
  row_grp_plan = row_grp_plan(label_loc =element_row_grp_loc("column")),
  col_plan = col_plan(
    `Pooled Id`,  `Site Id`,
    contains("Placebo"),
    contains("High Dose"),
    contains("Low Dose"),
    everything()
  )
) %>%
  print_to_gt(data)
Pooled Id Site Id Placebo (N=86) Total (N=254) Xanomeline High Dose (N=84) Xanomeline Low Dose (N=84)
ITT Eff Com ITT Eff Com ITT Eff Com ITT Eff Com
701 701 13 12 17 18 14 15  9 22 20  9 19 11
703 703 22 15 20 16 12 17 15 11 16 10 14 17
704 704 15 15 12 19 22  5 10 21 16 14 28 18
705 705 15 22  5 16 13 19 23 14 15 13 13 16
708 708 12 18 16 13 19 16 20 18 15 11 11 15
709 709 12 18 13  8 13 11 14 17 18 18 11  9
710 710 17 10 16 15 17 23  9 11 20  8 18  9
713 713 22 16 12 21  9 12 14  9 16  9 16 23
716 716 13 15 16 19 15 13 19 15 20 12 20 15
718 718 14 17 14 19 21 12 15 14 11 16 19 12
900 702 13 15 15 15 18 14 23 10 13 16 14 13
900 706 18 16 14 16 13 12 25 11 17 16 13  9
900 707 20 25 17 19 15 11 12 19 20 13 12 12
900 711 22 17 16 16 23 15  9 14 12 16  8  8
900 714  6 19 17 13 24 11 16 18 21 15 19 20
900 715 14 18 14 10 18 11 21  8 11 15 24 11
900 717 22 23 12 17 16 21 22 10 13 19 17 18
Total   17 11 15 15 18 18 17 18 14 10 10 11