R/dx_demographics.R

Defines functions dx_demographics

Documented in dx_demographics

#' A tools4ukbb function
#' 
#' Function output: a dataframe with data regarding sex and age demographics (count female, count male, count total, median and mean age)
#' Each row is an icd (or self reported) code and one additional row is added displaying the demographics for all combined icd10, icd9, and self-reported codes called (respectively)
#' 
#' @param icd_list a list of the icd10 codes you wish to investigate
#' @param dataframe the originial phenotype dataframe containing all individuals in the ukbiobank (~500,000 cols x 18,000 rows as of 09/07/2021)
#' @keywords demographics, sex, age
#' @export
#' @examples
#' dx_demographics()

dx_demographics <- function(icd_list, dataframe, ...){
    arguments <- list(...)

    icd_list_additional <- c(icd_list, list(icd_list))
    icd_labels <- c(icd_list, "Combined_ICD_Codes")

    icd_stats <- map(icd_list_additional, sex_age_stats, dataframe) %>% do.call(rbind, .) %>% mutate(dx_codes = icd_labels) 
    
    if(length(arguments$self_reported) > 0){
        sr_stats <- self_reported_counts(arguments$self_reported, dataframe) %>% mutate(dx_codes = paste("Self_Reported_", arguments$self_reported )) 
    } else { 
        sr_stats = data.frame() 
    }
    if (length(arguments$cause_of_death) > 0) {
        cod_stats <- cause_of_death_counts(arguments$cause_of_death, dataframe) %>% mutate(dx_codes = paste("Cause_of_Death_Included_", arguments$cause_of_death)) 
    } else {
        cod_stats = data.frame() 
    }

    final_stats_df <- bind_rows(icd_stats, sr_stats, cod_stats)
    final_stats_df
}
Lab-Jaiswal/tools4ukbb documentation built on May 12, 2022, 9:11 a.m.