From 05a14d2cb1d163b9c63af4c90027780da49c4a9a Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Wed, 6 Dec 2023 04:51:42 +0000 Subject: [PATCH] add reference df so that DM can be joined on the fly --- R/calculate_study_day.R | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 8db5ba51..8a6b9e87 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -1,9 +1,31 @@ #' `calculate_study_day` Performs the Study Day Calculation #' -calculate_study_day <- function(ds_in, refdt, tgdt) { +calculate_study_day <- function(ds_in, + ds_ref, + refdt, + tgdt, + merge_key = "USUBJID") { + assertthat::assert_that(is.data.frame(ds_in)) - assertthat::assert_that(hasName(ds_in, refdt)) + assertthat::assert_that(is.data.frame(ds_ref)) + assertthat::assert_that(hasName(ds_ref, refdt)) assertthat::assert_that(hasName(ds_in, tgdt)) + assertthat::assert_that(hasName(ds_ref, merge_key)) + assertthat::assert_that(hasName(ds_in, merge_key)) + + if (!identical(ds_in, ds_ref)) { + ds_ref <- unique(ds_ref[c(merge_key, refdt)]) + + check_refdt_uniqueness <- ds_ref %>% + dplyr::group_by(dplyr::pick({{merge_key}})) %>% + dplyr::filter(n() > 1) + if (nrow(check_refdt_uniqueness) > 0) { + stop("Reference date is not unique for each patient!") + } + + ds_in <- ds_in %>% + dplyr::left_join(ds_ref, by = structure(names = merge_key, .Data = merge_key)) + } # question: should I assume that refdt/tgdt was converted to Date already? # If assume that refdt and tgdt are already dates