Creating a Shift Table in R

Here is a workflow to create a Shift Table in R, using the {tidyverse} suite for data processing, and {gt} to build the desired table layout.

Required Packages

Code
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(tidyr))
suppressPackageStartupMessages(library(rlang))
suppressPackageStartupMessages(library(purrr))
suppressPackageStartupMessages(library(stringr))
suppressPackageStartupMessages(library(gt))
suppressPackageStartupMessages(library(here))

# list all files
files <- list.files(here("R"), pattern = ".R", full.names = TRUE)
# Read all files
walk(files, source)

Data used for Analysis

We will make use of the adsl and adlb test ADaM datasets from the {pharmaverseadam} R package for analysis.

ADSL is the subject level analysis dataset

Code
adsl <- pharmaverseadam::adsl

ADLB is the analysis dataset for Laboratory Records

Code
adlb <- pharmaverseadam::adlb

Variables used for Analysis

  • USUBJID - Unique Subject Identifier
  • SAFFL - Safety Population Flag
  • TRT01A - Actual Treatment Arm for Period 01
  • PARAM - Parameter
  • PARAMCD - Parameter Code
  • AVISIT - Analysis Visit
  • AVISITN - Analysis Visit (Numeric)
  • AVAL - Analysis Value
  • ANL01FL - Analysis Flag 01
  • BNRIND - Baseline Reference Range Indicator
  • ANRIND - Analysis Reference Range Indicator

Programming Flow

1. Calculating BIG N

  • Keep only safety subjects (SAFFL == 'Y') in adsl
  • Count number of subjects in the full safety analysis set within each treatment arm (TRT01A)
Code
adsl_bign <- adsl |>
  na_to_missing() |>
  filter(.data$SAFFL == "Y") |>
  select(all_of(c("USUBJID", "TRT01A"))) |>
  add_count(.data$TRT01A, name = "TRT_N")

2. Preprocessing Lab Records

  • Merge adsl_bign to adlb to add TRT_N
  • Filter out missing values in Baseline Reference Range Indicator (BNRIND), Analysis Reference Range Indicator (ANRIND) and Analysis Value (AVAL)
  • Subset the resulting data for subjects with post-does records where analysis flag (ANL01FL) is equal to 'Y'
  • Subset data to keep records within the time period (eg. Week 2, Week 4, Week 6) we want to see the shifts in Laboratory Tests
  • Add BIG N to treatment labels by concatenating TRT_N with TRT01A
Code
adlb_prep <- adlb |>
  na_to_missing() |>
  mutate(across(all_of(c("BNRIND", "ANRIND")), str_to_title)) |>
  left_join(adsl_bign, by = c("USUBJID", "TRT01A")) |>
  filter(
    .data$BNRIND != "<Missing>",
    .data$ANRIND != "<Missing>",
    !is.na(.data$AVAL),
    .data$ANL01FL == "Y",
    .data$AVISIT %in% c("Week 2", "Week 4", "Week 6")
  ) |>
  mutate(TRT_VAR = paste0(.data$TRT01A, "<br>(N=", .data$TRT_N, ")")) |>
  select(-TRT_N)


Subset adlb_prep to keep only Hemoglobin records

Code
adlb_hgb <- adlb_prep |>
  filter(.data$PARAMCD == "HGB")