Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

# 16 calculate_study_day #20

Merged
merged 75 commits into from
Mar 13, 2024
Merged
Changes from 1 commit
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
47e08c8
draft
Nov 6, 2023
05a14d2
add reference df so that DM can be joined on the fly
Dec 6, 2023
ef235ac
add roxygen doc
Dec 6, 2023
d3814cb
doc update
Dec 6, 2023
1333ca9
rename ds_in as sdtm_in
Jan 16, 2024
10c2233
replace ds_dm with dm_domain
Jan 17, 2024
2881071
add default value for refdt
Jan 17, 2024
4eb2926
add matching check
Jan 17, 2024
512e254
Merge branch 'main' into 16_calculate_study_day@devel
Jan 20, 2024
1caeb91
refdt and tgdt have to be ISO format, otherwise NA and warning
Jan 21, 2024
4cdf814
utils::hasName
Jan 21, 2024
e62d5dc
modify assert message
Jan 21, 2024
1c267e1
update message for refdt and tgdt convertion error
Jan 22, 2024
aecf81d
set .call to FALSE for warning
Jan 22, 2024
222c4b7
add unit test
Jan 22, 2024
b936cf1
update document
Jan 22, 2024
3cca05a
style update
Jan 22, 2024
5e41b4b
add asserthat in DESCRIPTION
Jan 22, 2024
a4fc269
Automatic renv profile update.
yli110-stat697 Jan 22, 2024
2e04f13
Automatic renv profile update.
yli110-stat697 Jan 22, 2024
72641d1
use data frame instead of data.frame
yli110-stat697 Feb 2, 2024
8036644
capitalize param per reference
yli110-stat697 Feb 2, 2024
d05a147
data frame instead of data.frame
yli110-stat697 Feb 2, 2024
c495170
capitalize param per reference
yli110-stat697 Feb 2, 2024
fa6667a
capitalize param per reference
yli110-stat697 Feb 2, 2024
6d19981
update wording for test
yli110-stat697 Feb 2, 2024
a0c6522
update wording for test
yli110-stat697 Feb 2, 2024
a90210f
use base pipe
yli110-stat697 Feb 2, 2024
79799db
use base pipe
yli110-stat697 Feb 2, 2024
381de67
use base pipe
yli110-stat697 Feb 2, 2024
2e63aef
Merge branch 'main' into 16_calculate_study_day@devel
yli110-stat697 Feb 2, 2024
0d7a179
Merge branch 'main' into 16_calculate_study_day@devel
yli110-stat697 Feb 9, 2024
c3ec9ce
update man file
Feb 11, 2024
01c3fc5
update df name for lint compliance
Feb 11, 2024
7f4a8bb
integer with L
Feb 11, 2024
a47e827
L for test integers
Feb 11, 2024
f6d467b
replace nested ifelse with case when per lintr suggestion
Feb 11, 2024
9ad8daf
Revert "replace nested ifelse with case when per lintr suggestion"
Feb 12, 2024
a1af5be
capitalize param per reference
yli110-stat697 Feb 12, 2024
0c78685
update typo in roxygen header
Feb 12, 2024
fd8e46c
resolve conflict
Feb 12, 2024
f20ed8b
capitalize param per reference
yli110-stat697 Feb 12, 2024
9c26b03
capitalize param per reference
yli110-stat697 Feb 12, 2024
8a977b2
remove don't run
Feb 12, 2024
44aef03
Merge branch '16_calculate_study_day@devel' of github.com:pharmaverse…
Feb 12, 2024
06532bd
capitalize param per reference
yli110-stat697 Feb 12, 2024
0be0d8b
use mapply to replace nested if else
Feb 12, 2024
4b79383
Merge branch '16_calculate_study_day@devel' of github.com:pharmaverse…
Feb 12, 2024
c3d812f
style
Feb 12, 2024
99cc8d2
roxygen update
Feb 12, 2024
9243268
fixed to be TRUE
Feb 12, 2024
df4a7ed
fix lintr
Feb 12, 2024
4e7c392
fix lintr in test file
Feb 12, 2024
026f5a3
remove @md tag
Feb 15, 2024
7e92d53
Update `calculate_study_day()`
ramiromagno Feb 17, 2024
d651028
move defaults to the end of argument positions
Feb 19, 2024
81d5804
merge from ramio
Feb 19, 2024
ae903e9
add equal sign
Feb 19, 2024
64dcdb1
default to "DM"
Feb 19, 2024
7c91722
update unit test per the arg position change
Feb 19, 2024
fa26e73
man file update
Feb 19, 2024
958453e
remove dm_domain default
Feb 21, 2024
b95a82a
Update common.yml
galachad Feb 27, 2024
9f739b7
simplify by statement
Mar 4, 2024
aadbae5
Merge branch '16_calculate_study_day@devel' of github.com:pharmaverse…
Mar 4, 2024
f424a58
Automatic renv profile update.
yli110-stat697 Mar 4, 2024
d57ad5a
add unit test for same input and reference df
Mar 4, 2024
b86965d
Merge branch '16_calculate_study_day@devel' of github.com:pharmaverse…
Mar 4, 2024
091e506
rename function
Mar 4, 2024
4379011
rename files
Mar 4, 2024
e6d8758
study day changed to integer
Mar 4, 2024
2037ba1
update style
Mar 4, 2024
48ba06d
update man file
Mar 4, 2024
2074b16
update doc
yli110-stat697 Mar 11, 2024
fcdcb0c
update man file
Mar 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add roxygen doc
  • Loading branch information
Rosemary Li committed Dec 6, 2023
commit ef235acaa5c838dd36a3786bac132f2f385792d6
81 changes: 68 additions & 13 deletions R/calculate_study_day.R
yli110-stat697 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,36 +1,90 @@
#' `calculate_study_day` Performs the Study Day Calculation
#' `calculate_study_day` performs study day calculation
#' @description
#' This function takes the an input data.frame and a reference data.frame (which
#' is DM domain in most cases), and calculate the study day from reference date
yli110-stat697 marked this conversation as resolved.
Show resolved Hide resolved
#' and target date. In case of unexpected conditions like reference date is not
#' unique for each patient, or reference and input dates are not actual dates,
#' NA will be returned for those records.
#'
#' @md
#' @param ds_in input data.frame that contains the target date.
#' @param ds_dm reference date.frame that contains the reference date.
#' @param refdt reference date from `ds_dm` that will be used as reference to
#' calculate the study day.
#' @param tgdt target date from `ds_in` that will be used to calcualte the study
#' day.
#' @param study_day_var the new study day variable name in the output. For
#' example, AESTDY for AE domain for CMSTDY for CM domain.
yli110-stat697 marked this conversation as resolved.
Show resolved Hide resolved
#' @param merge_key character to represents the merging key between `ds_in` and
yli110-stat697 marked this conversation as resolved.
Show resolved Hide resolved
#' `ds_dm`.
#'
#' @return a data.frame that takes all columns from `ds_in` and a new variable
#' to represent the calculated study day.
#'
#' @examples
#' \dontrun{
#' ae <- data.frame(
yli110-stat697 marked this conversation as resolved.
Show resolved Hide resolved
#' USUBJID = c("study123-123", "study123-124", "study123-125"),
#' AESTDTC = c("2012-01-01", "2012-04-14", "2012-04-14")
#' )
#' dm <- data.frame(
#' USUBJID = c("study123-123", "study123-124", "study123-125"),
#' RFSTDTC = c("2012-02-01", "2012-04-14", NA)
#' )
#' ae$AESTDTC <- as.Date(ae$AESTDTC)
#' dm$RFSTDTC <- as.Date(dm$RFSTDTC)
#' calculate_study_day(ae, dm, "RFSTDTC", "AESTDTC", "AESTDY")
#' }
#'
#'

calculate_study_day <- function(ds_in,
ds_ref,
yli110-stat697 marked this conversation as resolved.
Show resolved Hide resolved
ds_dm,
refdt,
yli110-stat697 marked this conversation as resolved.
Show resolved Hide resolved
tgdt,
yli110-stat697 marked this conversation as resolved.
Show resolved Hide resolved
study_day_var,
merge_key = "USUBJID") {

assertthat::assert_that(is.data.frame(ds_in))
assertthat::assert_that(is.data.frame(ds_ref))
assertthat::assert_that(hasName(ds_ref, refdt))
assertthat::assert_that(is.data.frame(ds_dm))
assertthat::assert_that(hasName(ds_dm, refdt))
assertthat::assert_that(hasName(ds_in, tgdt))
assertthat::assert_that(hasName(ds_ref, merge_key))
yli110-stat697 marked this conversation as resolved.
Show resolved Hide resolved
assertthat::assert_that(hasName(ds_dm, merge_key))
assertthat::assert_that(hasName(ds_in, merge_key))
assertthat::assert_that(is.character(study_day_var))

if (!identical(ds_in, ds_ref)) {
ds_ref <- unique(ds_ref[c(merge_key, refdt)])
if (!identical(ds_in, ds_dm)) {
ds_dm <- unique(ds_dm[c(merge_key, refdt)])

check_refdt_uniqueness <- ds_ref %>%
check_refdt_uniqueness <- ds_dm %>%
dplyr::group_by(dplyr::pick({{merge_key}})) %>%
dplyr::filter(n() > 1)
dplyr::filter(dplyr::n() > 1)
if (nrow(check_refdt_uniqueness) > 0) {
stop("Reference date is not unique for each patient!")
warning(
"Reference date is not unique for each patient! ",
"Patient without unique reference date will be ingored. ",
yli110-stat697 marked this conversation as resolved.
Show resolved Hide resolved
"NA will be returned for such records."
)
ds_dm <- ds_dm[
!ds_dm[[merge_key]] %in% check_refdt_uniqueness[[merge_key]],
]
}

ds_in <- ds_in %>%
dplyr::left_join(ds_ref, by = structure(names = merge_key, .Data = merge_key))
dplyr::left_join(
ds_dm, 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
if (!("Date" %in% class(ds_in[[refdt]]) && "Date" %in% class(ds_in[[tgdt]]))) {
stop("Reference and target date has to be Date objects.")
warning(
"Reference and target date has to be Date objects. ",
"If either is not, NA will be returned for study day."
)
ds_in[study_day_var] <- NA
return(ds_in)
}
refdt_vector <- ds_in[[refdt]]
tgdt_vector <- ds_in[[tgdt]]
@@ -44,6 +98,7 @@ calculate_study_day <- function(ds_in,
no = NA
)
)
return(res)
ds_in[study_day_var] <- res
return(ds_in)
}

Loading