Learn R Programming

StatsTFLValR (version 1.0.0)

freq_by_line: One-Line Frequency Summary by Treatment Group

Description

Generates a single-row frequency summary table across treatment groups, reporting counts and percentages of subjects meeting a filter condition.

Usage

freq_by_line(data, id_var, trt_var, filter_expr, label, denom_data = NULL)

Value

A one-row tibble containing "n (pct)" summaries per treatment group.

Arguments

data

A data.frame containing subject-level data.

id_var

Unquoted subject ID variable (e.g., USUBJID).

trt_var

Unquoted treatment variable (e.g., TRT01P).

filter_expr

A logical filter expression (unquoted), e.g., SAFFL == "Y" & AGE >= 65.

label

Character string for the row label in the output (e.g., "SAF population").

denom_data

Optional. A data.frame used to calculate denominators per treatment group. Defaults to data.

Details

This function calculates the number and percentage of unique subjects per treatment group (trt_var) satisfying a given filter condition (filter_expr). The result is formatted as "n (pct)" and returned in a single-row tibble, labeled by the provided label. An optional denominator dataset (denom_data) can be specified to override the default denominator population (used to calculate percentages).

Useful for producing compact summary rows (e.g., "SAF Population", "Subjects >= 65") in clinical tables.

Examples

Run this code
set.seed(123)
adsl <- data.frame(
  USUBJID = paste0("SUBJ", 1:100),
  TRT01P = sample(c("0", "54", "100"), 100, replace = TRUE),
  SAFFL = sample(c("Y", "N"), 100, replace = TRUE),
  AGE = sample(18:80, 100, replace = TRUE)
)


freq_by_line(adsl, USUBJID, TRT01P, SAFFL == "Y", label = "SAF population")


saf <- adsl[adsl$SAFFL == "Y", ]

freq_by_line(
  adsl, USUBJID, TRT01P,
  AGE >= 65,
  label = "Age >=65 in SAF",
  denom_data = saf
)

Run the code above in your browser using DataLab