Skip to content

assign_quartile() function #54

@callistosp

Description

@callistosp

Problem: existing functions in R which assign quartiles will assign different quartiles to the same numeric value to force an equal number of values into each quartile. Instead, we would like a function that will calculate the quartile values and assigns values to each quartile while ensuring that the same value will not appear in multiple quartiles, resulting in an interpretable numeric range for each quartile.

Proposed solution: this function was adapted from @graceannobrien's code on a recent project

assign_quartile <- function(.df, .cov){
  qt <- .df %>% 
    distinct(USUBJID,{{.cov}}) %>% 
    mutate(
      COV_DRANK = dense_rank({{.cov}}),
      COV_DRANK_MAX = max(COV_DRANK, na.rm = T),
      COV_DRANK_FINAL = COV_DRANK/COV_DRANK_MAX,
      COVQ = case_when(
        COV_DRANK_FINAL <= 0.25 ~ 1,
        COV_DRANK_FINAL <= 0.5 & COV_DRANK_FINAL > 0.25 ~ 2,
        COV_DRANK_FINAL <= 0.75 & COV_DRANK_FINAL > 0.5 ~ 3,
        COV_DRANK_FINAL > 0.75 ~ 4
      )
    ) %>% select(USUBJID, COVQ)
  
  names(qt)[2] <- paste0(str_to_upper(deparse(substitute(.cov))),"Q")
  
  return(full_join(.df, qt, by="USUBJID"))
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions