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 14 19 17 21 11 15 17 15 17 15  9 14
703 703 10 16 14 22 17 14 13  8 17 15 12 13
704 704 11 14 22 16  9 14 12  8 23 13 18 13
705 705 13 14 12 14 18 20  5 12 12 12 18 11
708 708 13 20 23 10 11 19 17 18 13  7 15  7
709 709 19 20 17 14 12 12 19 19 15 13 18 16
710 710 13 25 16 10 18 16 18 15 13 20 16 17
713 713 14 18 25  6 11 16 14  8 16 13  9 14
716 716 15 15 12 13 16 14 20 19 12 11 13 17
718 718  9 16 16 10 14 13 19 10 13 10 13 19
900 702 14 15 24 15 12 13 16 15 11 11  8  9
900 706 19  8 12 13  8 16 12  9 17 12 20 14
900 707 17 11 14  8 18 19 17 12 10 21 25  5
900 711 12 17 15 17 20 15 17 16 13 15 14 17
900 714 16 15 17 13 18 17 15 21  8 12 12 12
900 715 11 20 12 12 19 21 15 20 17 27 18 14
900 717 12 22 17 12 16 11 13 16 12 22 15 15
Total   24  9 18 12 15 14 15 15  9 12 11 19