Create Demographic table

Introduction

This guide will show you how two pharmaverse packages, along with some from tidyverse, can be used to create a Demographic table as an end-to-end, using the {admiral} ADSL data as an input.

The two packages used with a brief description of their purpose are as follows:

  • {rtables}: designed to create and display complex tables with R.
  • {tern}: contains analysis functions to create tables and graphs used for clinical trial reporting.

It is important to understand {rtables} grammar by reading through the above linked package site, as these are fundamental to being able to use {tern}.

Load Data and Required pharmaverse Packages

After installation of packages, the first step is to load our pharmaverse packages and input data.

options(repos = c(
  pharmaverse = 'https://pharmaverse.r-universe.dev',
  CRAN = 'https://cloud.r-project.org'))

library(admiral)
library(rtables)
library(tern)
library(dplyr)

# Read in input ADaM data 
data("admiral_adsl")

Start preprocessing

The first step we are going to encode missing entries across groups of categorical variables in a data frame adsl.

adsl <- df_explicit_na(admiral_adsl)

Now we will add some pre-processing.

adsl <- adsl %>%
  mutate(
    SEX = factor(case_when(
      SEX == "M" ~ "Male",
      SEX == "F" ~ "Female",
      SEX == "U" ~ "Unknown",
      SEX == "UNDIFFERENTIATED" ~ "Undifferentiated"
    ))
  ) 

Now optionally we create lists: vars and var_labels which hold the variables and thier corresponding labels. We will use them in the next step.

vars <- c("AGE", "AGEGR1", "SEX","RACE")

var_labels <- c(
  "Age (yr)",
  "Age groups",
  "Sex",
  "Race"
)

Now we create the demographic table using the {rtables} and {tern} packages, and using ARM as the grouping variable.

result <- basic_table() %>%
  split_cols_by(var = "ARM") %>%
  add_colcounts() %>%
  summarize_vars(
    vars = vars,
    var_labels = var_labels
  ) %>%
  build_table(adsl)

result
##                                        Placebo     Screen Failure   Xanomeline High Dose   Xanomeline Low Dose
##                                        (N=86)          (N=52)              (N=84)                (N=84)       
## ——————————————————————————————————————————————————————————————————————————————————————————————————————————————
## Age (yr)                                                                                                      
##   n                                      86              52                  84                    84         
##   Mean (SD)                          75.2 (8.6)      75.1 (9.7)          74.4 (7.9)            75.7 (8.3)     
##   Median                                76.0            76.0                76.0                  77.5        
##   Min - Max                          52.0 - 89.0    50.0 - 89.0         56.0 - 88.0            51.0 - 88.0    
## Age groups                                                                                                    
##   n                                      86              52                  84                    84         
##   <18                                     0              0                   0                      0         
##   18-64                              14 (16.3%)      9 (17.3%)           11 (13.1%)             8 (9.5%)      
##   >=65                               72 (83.7%)      43 (82.7%)          73 (86.9%)            76 (90.5%)     
## Sex                                                                                                           
##   n                                      86              52                  84                    84         
##   Female                             53 (61.6%)      36 (69.2%)          40 (47.6%)            50 (59.5%)     
##   Male                               33 (38.4%)      16 (30.8%)          44 (52.4%)            34 (40.5%)     
## Race                                                                                                          
##   n                                      86              52                  84                    84         
##   AMERICAN INDIAN OR ALASKA NATIVE        0           1 (1.9%)            1 (1.2%)                  0         
##   ASIAN                                   0           2 (3.8%)               0                      0         
##   BLACK OR AFRICAN AMERICAN           8 (9.3%)       6 (11.5%)           9 (10.7%)              6 (7.1%)      
##   WHITE                              78 (90.7%)      43 (82.7%)          74 (88.1%)            78 (92.9%)