benjaminrich / table1

78 stars 26 forks source link

Handing multiple checkboxes variable #124

Open pbilodeau94 opened 3 months ago

pbilodeau94 commented 3 months ago

Hi! Very nice package. I'm working with survey data that has multiple checkboxes variable, where dummy variables are created for each factor e.g comorbidities___1, 2, 3, etc. I tried using the solution from #98 and it correctly creates the header and subcategories, but I have not been able to pull the data. image

label (demographics_table$Age_at_nmo_diagnosis) <- "Age at NMO diagnosis" label (demographics_table$Follow_up_time) <- "Follow-up (in years)" demographics_table$comorbidities <- 1:nrow (demographics_table) label(demographics_table$comorbidities) <- "Comorbidities, n (%)"

rndr <- function(x, name, ...) { if (name == "comorbidities") { vars <- c("Autoimmune disease", "Psychiatric disorders (anxiety or depression, mania, psychosis)", "Neurologic disorders (including migraines)", "Vascular risk factors (hypertension, diabetes, hyperlipidemia)", "Coronary artery disease or ischemic heart disease", "Chronic obstructive pulmonary disease or asthma", "Chronic kidney disease", "Cancer", "None") y <- sapply(vars, function(var) { render.default(dat[[var]][x])[2] }) names(y) <- sapply(vars, function(var) { l <- label(dat$var) if (is.null(l)) var else l }) c("", y) } else {

For numeric variables, perform Shapiro-Wilk test and render based on its result

if (is.numeric(x)) {
  shapiro_test_result <- shapiro.test(x)
  what <- ifelse(shapiro_test_result$p.value > 0.05,
                 "Mean (SD)",  # If p-value > 0.05, data is considered normally distributed
                 "Median [IQR]")  # If p-value <= 0.05, data is considered not normally distributed
  parse.abbrev.render.code(c("", what))(x)
} else {
  # For non-numeric variables, use default rendering
  render.default(x, name, ...)
}

} } table1(~ Age + sex + ethnicity + race + bmi + Age_at_nmo_diagnosis + Follow_up_time + living_deceased + mrs_lastfu + comorbidities| aqp4, data = demographics_table, render = rndr)

Do you know of an easy way to show those variables in the table?