From fae9603df200382dc0f11477b91ce07f21bd16c4 Mon Sep 17 00:00:00 2001 From: prockenschaub Date: Fri, 15 Mar 2024 15:58:33 +0100 Subject: [PATCH 1/2] Fix timediff for AUMCdb --- R/data-load.R | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/R/data-load.R b/R/data-load.R index 80b57ee4..6700bf33 100644 --- a/R/data-load.R +++ b/R/data-load.R @@ -131,7 +131,7 @@ load_difftime.eicu_tbl <- function(x, rows, cols = colnames(x), warn_dots(...) - load_eiau(x, {{ rows }}, cols, id_hint, time_vars, min_as_mins) + load_eisi(x, {{ rows }}, cols, id_hint, time_vars, min_as_mins) } #' @rdname load_src @@ -153,7 +153,7 @@ load_difftime.aumc_tbl <- function(x, rows, cols = colnames(x), warn_dots(...) - load_eiau(x, {{ rows }}, cols, id_hint, time_vars, ms_as_mins) + load_au(x, {{ rows }}, cols, id_hint, time_vars) } #' @rdname load_src @@ -175,7 +175,7 @@ load_difftime.sic_tbl <- function(x, rows, cols = colnames(x), sec_as_mins <- function(x) min_as_mins(as.integer(x / 60)) warn_dots(...) - load_eiau(x, {{ rows }}, cols, id_hint, time_vars, sec_as_mins) + load_eisi(x, {{ rows }}, cols, id_hint, time_vars, sec_as_mins) } #' @rdname load_src @@ -237,7 +237,7 @@ load_mihi <- function(x, rows, cols, id_hint, time_vars) { as_id_tbl(dat, id_vars = id_col, by_ref = TRUE) } -load_eiau <- function(x, rows, cols, id_hint, time_vars, mins_fun) { +load_eisi <- function(x, rows, cols, id_hint, time_vars, mins_fun) { id_col <- resolve_id_hint(x, id_hint) @@ -259,6 +259,35 @@ load_eiau <- function(x, rows, cols, id_hint, time_vars, mins_fun) { as_id_tbl(dat, id_vars = id_col, by_ref = TRUE) } +load_au <- function(x, rows, cols, id_hint, time_vars) { + dt_round_min <- function(x, y) round_to(ms_as_mins(x - y)) + + id_col <- resolve_id_hint(x, id_hint) + + assert_that(is.string(id_col), id_col %in% colnames(x)) + + if (!id_col %in% cols) { + cols <- c(cols, id_col) + } + + time_vars <- intersect(time_vars, cols) + + dat <- load_src(x, {{ rows }}, cols) + + if (length(time_vars)) { + + dat <- merge(dat, id_origin(x, id_col, origin_name = "origin"), + by = id_col) + dat <- dat[, + c(time_vars) := lapply(.SD, dt_round_min, get("origin")), + .SDcols = time_vars + ] + dat <- dat[, c("origin") := NULL] + } + + as_id_tbl(dat, id_vars = id_col, by_ref = TRUE) +} + #' Load data as `id_tbl` or `ts_tbl` objects #' #' Building on functionality provided by [load_src()] and [load_difftime()], From 09239e44d04cd52f5fde61019133994dcffbd8c7 Mon Sep 17 00:00:00 2001 From: prockenschaub Date: Fri, 15 Mar 2024 16:30:34 +0100 Subject: [PATCH 2/2] fix bug in trunc_time that prevents truncation --- R/utils-ts.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/utils-ts.R b/R/utils-ts.R index 74afa62a..b9b1fa7d 100644 --- a/R/utils-ts.R +++ b/R/utils-ts.R @@ -643,11 +643,11 @@ padded_capped_diff <- function(x, final, max) { trunc_time <- function(x, min, max) { if (not_null(min)) { - replace(x, x < min, min) + x <- replace(x, x < min, min) } if (not_null(max)) { - replace(x, x > max, max) + x <- replace(x, x > max, max) } x