Skip to content

Commit

Permalink
refactor: ♻️ function to create LPR2
Browse files Browse the repository at this point in the history
Based on updates from #133
  • Loading branch information
lwjohnst86 committed Sep 20, 2024
1 parent 3a1e66f commit 0ad86d1
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 23 deletions.
79 changes: 79 additions & 0 deletions R/create-lpr2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#' Create the LPR2 data needed for later purposes.
#'
#' Needs the LPR2 (`lpr_diag` and `lpr_adm`) registers.
#'
#' @param lpr_diag The diagnosis register.
#' @param lpr_adm The admission register.
#'
#' @return The same class as the input, defaults to a [tibble::tibble()].
#' @keywords internal
#'
#' @examples
#' \dontrun{
#' create_lpr2(
#' lpr_adm = register_data$lpr_adm,
#' lpr_diag = register_data$lpr_diag
#' )
#' }
create_lpr2 <- function(lpr_adm, lpr_diag) {
verify_required_variables(lpr_adm, "lpr_adm")
verify_required_variables(lpr_diag, "lpr_diag")
lpr_diag <- lpr_diag |>
column_names_to_lower() |>
keep_needed_diagnosis_code() |>
keep_needed_diagnosis_type()

join_lpr2(
lpr_diag = lpr_diag,
lpr_adm = column_names_to_lower(lpr_adm)
) |>
dplyr::mutate(
is_primary = .data$c_diagtype == "A",
# TODO: Include this in algorithm dataframe some how?
is_t1d = stringr::str_detect(.data$c_diag, "^(249|DE10)"),
is_t2d = stringr::str_detect(.data$c_diag, "^(250|DE11)"),
department = get_department(.data$c_spec),
date = yyyymmdd_to_iso(.data$d_inddto)
) |>
dplyr::select(
"pnr",
"date",
"is_primary",
"is_t1d",
"is_t2d",
"department"
)
}

keep_needed_diagnosis_code <- function(data) {
# TODO: Include this in algorithm dataframe some how?
data |>
dplyr::filter(stringr::str_detect(.data$c_diag, "^(DO|DZ3|DE1[0-4]|249|250)"))
}

keep_needed_diagnosis_type <- function(data) {
# TODO: Include this in algorithm dataframe some how?
data |>
dplyr::filter(.data$c_diagtype %in% c("A", "B"))
}

join_lpr2 <- function(lpr_adm, lpr_diag) {
dplyr::inner_join(
lpr_adm,
lpr_diag,
by = "recnum"
)
}

get_department <- function(x) {
dplyr::case_when(
# TODO: Include this in algorithm dataframe some how?
x == "08" ~ "endocrinology",
# < 8 and between 9-30
stringr::str_detect(x, "(0[0-8]|9|[1-3][0-9])") ~ "other medical"
)
}

yyyymmdd_to_iso <- function(x) {
date = lubridate::as_date(x, format = "%Y%m%d")
}
23 changes: 0 additions & 23 deletions R/joins.R
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
#' Join together the LPR2 (`lpr_diag` and `lpr_adm`) registers.
#'
#' @param lpr_diag The diagnosis register.
#' @param lpr_adm The admission register.
#'
#' @return The same class as the input, defaults to a [tibble::tibble()].
#' @keywords internal
#'
#' @examples
#' \dontrun{
#' register_data$lpr_adm |>
#' join_lpr2(register_data$lpr_diag)
#' }
join_lpr2 <- function(lpr_adm, lpr_diag) {
verify_required_variables(lpr_adm, "lpr_adm")
verify_required_variables(lpr_diag, "lpr_diag")
dplyr::inner_join(
column_names_to_lower(lpr_adm),
column_names_to_lower(lpr_diag),
by = "recnum"
)
}

#' Join together the LPR3 (`diagnoser` and `kontakter`) registers.
#'
#' @param diagnoser The diagnosis register.
Expand Down

0 comments on commit 0ad86d1

Please sign in to comment.