-
Notifications
You must be signed in to change notification settings - Fork 3
Open
Description
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"))
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels