From 8b732762e0e6a2b3a2298dcf4084a5abb7c2143a Mon Sep 17 00:00:00 2001 From: Emily de la Rua Date: Mon, 27 Nov 2023 15:12:25 -0500 Subject: [PATCH] Allow numeric input for `count_by` in `analyze_num_patients` --- NEWS.md | 1 + R/summarize_num_patients.R | 14 +++++--------- man/summarize_num_patients.Rd | 8 ++++---- tests/testthat/test-summarize_num_patients.R | 14 +++++++------- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/NEWS.md b/NEWS.md index 19496c2b8c..785a40e77c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -18,6 +18,7 @@ * Added `annot_coxph_ref_lbls` parameter to `g_km` to enable printing the reference group in table labels when `annot_coxph = TRUE`. * Added `x_lab` parameter to `g_lineplot` to customize x-axis label. * Remove 25% padding of y-axis in `g_lineplot`. +* Allowed numeric vector as `count_by` input in `analyze_num_patients` and `summarize_num_patients`. ### Bug Fixes * Fixed bug in `decorate_grob` preventing text wrapping from accounting for font size. diff --git a/R/summarize_num_patients.R b/R/summarize_num_patients.R index 36149168d3..137bd7d175 100644 --- a/R/summarize_num_patients.R +++ b/R/summarize_num_patients.R @@ -5,8 +5,8 @@ #' Count the number of unique and non-unique patients in a column (variable). #' #' @inheritParams argument_convention -#' @param count_by (`character` or `factor`)\cr optional vector to be combined with `x` when counting -#' `nonunique` records. +#' @param count_by (`vector`)\cr optional vector of any type to be combined with `x` when counting `nonunique` +#' records. #' @param unique_count_suffix (`logical`)\cr should `"(n)"` suffix be added to `unique_count` labels. #' Defaults to `TRUE`. #' @param .stats (`character`)\cr statistics to select for the table. Run `get_stats("summarize_num_patients")` @@ -35,7 +35,7 @@ NULL #' x = as.character(c(1, 1, 1, 2, 4, NA)), #' labelstr = "", #' .N_col = 6L, -#' count_by = as.character(c(1, 1, 2, 1, 1, 1)) +#' count_by = c(1, 1, 2, 1, 1, 1) #' ) #' #' @export @@ -51,7 +51,6 @@ s_num_patients <- function(x, labelstr, .N_col, count_by = NULL, unique_count_su if (!is.null(count_by)) { checkmate::assert_vector(count_by, len = length(x)) - checkmate::assert_multi_class(count_by, classes = c("factor", "character")) count2 <- n_available(unique(interaction(x, count_by))) } @@ -86,7 +85,7 @@ s_num_patients <- function(x, labelstr, .N_col, count_by = NULL, unique_count_su #' #' df_by_event <- data.frame( #' USUBJID = as.character(c(1, 2, 1, 4, NA)), -#' EVENT = as.character(c(10, 15, 10, 17, 8)) +#' EVENT = c(10, 15, 10, 17, 8) #' ) #' s_num_patients_content(df_by_event, .N_col = 5, .var = "USUBJID", count_by = "EVENT") #' @@ -112,10 +111,7 @@ s_num_patients_content <- function(df, } x <- df[[.var]] - y <- switch(as.numeric(!is.null(count_by)) + 1, - NULL, - df[[count_by]] - ) + y <- if (is.null(count_by)) NULL else df[[count_by]] s_num_patients( x = x, diff --git a/man/summarize_num_patients.Rd b/man/summarize_num_patients.Rd index c5bcb7bc60..4ea0cc22b9 100644 --- a/man/summarize_num_patients.Rd +++ b/man/summarize_num_patients.Rd @@ -68,8 +68,8 @@ s_num_patients_content( \item{required}{(\code{character} or \code{NULL})\cr optional name of a variable that is required to be non-missing.} -\item{count_by}{(\code{character} or \code{factor})\cr optional vector to be combined with \code{x} when counting -\code{nonunique} records.} +\item{count_by}{(\code{vector})\cr optional vector of any type to be combined with \code{x} when counting \code{nonunique} +records.} \item{unique_count_suffix}{(\code{logical})\cr should \code{"(n)"} suffix be added to \code{unique_count} labels. Defaults to \code{TRUE}.} @@ -195,7 +195,7 @@ s_num_patients( x = as.character(c(1, 1, 1, 2, 4, NA)), labelstr = "", .N_col = 6L, - count_by = as.character(c(1, 1, 2, 1, 1, 1)) + count_by = c(1, 1, 2, 1, 1, 1) ) # Count number of unique and non-unique patients. @@ -208,7 +208,7 @@ s_num_patients_content(df, .N_col = 5, .var = "USUBJID") df_by_event <- data.frame( USUBJID = as.character(c(1, 2, 1, 4, NA)), - EVENT = as.character(c(10, 15, 10, 17, 8)) + EVENT = c(10, 15, 10, 17, 8) ) s_num_patients_content(df_by_event, .N_col = 5, .var = "USUBJID", count_by = "EVENT") diff --git a/tests/testthat/test-summarize_num_patients.R b/tests/testthat/test-summarize_num_patients.R index 4afed9e40b..2d0d38755e 100644 --- a/tests/testthat/test-summarize_num_patients.R +++ b/tests/testthat/test-summarize_num_patients.R @@ -83,7 +83,7 @@ testthat::test_that("summarize_num_patients works as expected with healthy input testthat::test_that("s_num_patients count_by works as expected with healthy input", { x <- as.character(c(1, 2, 1, 4, 1)) - y <- as.character(c(6, 7, 8, 9, 6)) + y <- c(6, 7, 8, 9, 6) result <- s_num_patients(x = x, labelstr = "", .N_col = 5, count_by = y) res <- testthat::expect_silent(result) @@ -92,7 +92,7 @@ testthat::test_that("s_num_patients count_by works as expected with healthy inpu testthat::test_that("s_num_patients count_by with missing works as expected with healthy input", { x <- as.character(c(1, 2, 1, 4, NA)) - y <- as.character(c(6, 7, 8, 9, 6)) + y <- c(6, 7, 8, 9, 6) result <- s_num_patients(x = x, labelstr = "", .N_col = 5, count_by = y) res <- testthat::expect_silent(result) @@ -101,7 +101,7 @@ testthat::test_that("s_num_patients count_by with missing works as expected with testthat::test_that("s_num_patients count_by with missing case 2 works as expected with healthy input", { x <- as.character(c(1, 2, 1, 4, 1)) - y <- as.character(c(6, 7, NA, 9, 6)) + y <- c(6, 7, NA, 9, 6) result <- s_num_patients(x = x, labelstr = "", .N_col = 5, count_by = y) res <- testthat::expect_silent(result) @@ -111,7 +111,7 @@ testthat::test_that("s_num_patients count_by with missing case 2 works as expect testthat::test_that("s_num_patients_content with count_by works as expected with healthy input", { df <- data.frame( USUBJID = as.character(c(1, 2, 1, 4, NA)), - AGE = as.character(c(10, 15, 10, 17, 8)) + AGE = c(10, 15, 10, 17, 8) ) result <- s_num_patients_content(df = df, .N_col = 5, .var = "USUBJID", count_by = "AGE") @@ -122,7 +122,7 @@ testthat::test_that("s_num_patients_content with count_by works as expected with testthat::test_that("s_num_patients_content with count_by case 2 works as expected with healthy input", { df <- data.frame( USUBJID = as.character(c(1, 2, 1, 4, NA)), - AGE = as.character(c(10, 15, 11, 17, 8)) + AGE = c(10, 15, 11, 17, 8) ) result <- s_num_patients_content(df = df, .N_col = 5, .var = "USUBJID", count_by = "AGE") @@ -133,7 +133,7 @@ testthat::test_that("s_num_patients_content with count_by case 2 works as expect testthat::test_that("s_num_patients_content with count_by trivial cases, identical to without count_by", { df <- data.frame( USUBJID = as.character(c(1, 2, 1, 4, 9)), - AGE = as.character(c(10, 15, 11, 17, 8)) + AGE = c(10, 15, 11, 17, 8) ) result <- s_num_patients_content(df = df, .N_col = 5, .var = "USUBJID", count_by = "USUBJID") @@ -145,7 +145,7 @@ testthat::test_that("summarize_num_patients with count_by works as expected with df <- data.frame( USUBJID = as.character(c(1, 2, 1, 4, NA, 6, 6, 8, 9)), ARM = c("A", "A", "A", "A", "A", "B", "B", "B", "B"), - BY = as.character(c(10, 15, 10, 17, 8, 11, 11, 19, 17)) + BY = c(10, 15, 10, 17, 8, 11, 11, 19, 17) ) # Check with both output