R/pull_demographics.R

Defines functions pull_demographics

Documented in pull_demographics

#' Extract demographics data from table returned by prepare_demographics_sc
#'
#' @param demographics_sc_tbl Tibble returned by the
#' prepare_demographics_sc function
#'
#' @return Tibble with only demographics data
#' @export
#'
#' @importFrom magrittr "%>%"

pull_demographics <- function(demographics_sc_tbl) {
  stopifnot(all(c(
    "patient_id",
    "birth_year",
    "race",
    "ethnicity",
    "collection_date"
  ) %in% colnames(demographics_sc_tbl)))

  output_tbl <- demographics_sc_tbl %>%
    dplyr::arrange(patient_id, dplyr::desc(collection_date)) %>%
    dplyr::select(
      "patient_id",
      "birth_year",
      "race",
      "ethnicity"
    ) %>%
    dplyr::filter(!is.na(patient_id)) %>%
    dplyr::distinct()


  output_tbl <- tidy_up_race(output_tbl)

  return(output_tbl)
}
CUGBF/deidentifiedDB documentation built on Sept. 13, 2023, 6:28 a.m.