Closed shijiaSMH closed 3 months ago
derive_n_hosp_prev <- function(ipdad, n, admit_date="admission_date_time", disch_date="discharge_date_time") { dat <- ipdad[, .(genc_id, patient_id_hashed, admit_date = get(admit_date), disch_date = get(disch_date))] dat[, patient_id_hashed := ifelse(is.na(patient_id_hashed) | patient_id_hashed =="", NA, patient_id_hashed)] dat[, ":=" (admit_date = ymd_hm(admit_date), disch_date = ymd_hm(disch_date))] dat <- dat %>% dplyr::group_by(patient_id_hashed) %>% dplyr::arrange(desc(admit_date), desc(disch_date)) %>% dplyr::ungroup() %>% data.table() dat[, ":=" (prev_disch1 = shift(disch_date, type = "lead", n = 1), prev_disch2 = shift(disch_date, type = "lead", n = 2)), by=patient_id_hashed] dat[, ":=" (td1 = as.numeric(difftime(admit_date, prev_disch1, units="days")/365.25), td2 = as.numeric(difftime(admit_date, prev_disch2, units="days")/365.25))] dat[, n_disch_past_6mo := dplyr::case_when(td2 <= n/365 ~ "2+", td1 <= n/365 ~ "1", is.na(td1) | td1 > n/365 ~ "0")] %>% .[is.na(patient_id_hashed), n_disch_past := NA] res <- dat[, c("genc_id", "n_disch_past")] return(res) }
Please describe in detail the functionality you would like to have added to the package. Describe inputs and outputs, and provide test examples if possible:
Please mention (if applicable), any research projects for which this functionality was needed: