Skip to content

Commit

Permalink
add reference df so that DM can be joined on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosemary Li committed Dec 6, 2023
1 parent 47e08c8 commit 05a14d2
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions R/calculate_study_day.R
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 05a14d2

Please sign in to comment.