Skip to content

Instantly share code, notes, and snippets.

@kyleGrealis
Last active December 19, 2024 17:05
Show Gist options
  • Select an option

  • Save kyleGrealis/ec54e9e506c3ad5b90a5972a2ddd6fe3 to your computer and use it in GitHub Desktop.

Select an option

Save kyleGrealis/ec54e9e506c3ad5b90a5972a2ddd6fe3 to your computer and use it in GitHub Desktop.
table1 tweaks
# set up including p-values and strata %
pvalue <- function(x, ...) {
# Construct vectors of data y, and groups (strata) g
y <- unlist(x)
g <- factor(rep(1:length(x), times=sapply(x, length)))
if (is.numeric(y)) {
# For numeric variables, perform ANOVA
### YOU MUST CHANGE THE DATASET NAMED BELOW!! ###
# p <- round(unlist(summary(aov(y ~ g, data = CHANGE_ME)))[9], digits = 3)
p <- t.test(y ~ g)$p.value
} else {
# For categorical variables, perform a chi-squared test of independence
p <- round(chisq.test(table(y, g))$p.value, digits = 3)
}
if (p > 0.5) { p = round(p, 2) }
if (p == 1) { p = 0.99 }
# Format the p-value, using an HTML entity for the less-than sign.
# The initial empty string places the output on the line below the variable label.
c("", sub("<", "< ", format.pval(p, digits=3, eps=0.001)))
}
# function to add percentage to table header row
render.strat <- function (label, n, ...) {
overall.n <- sum(as.numeric(n)[names(n)!="overall"], na.rm = TRUE)
pct <- 100*as.numeric(n)/overall.n
pct <- round_pad(pct, 0)
sprintf(
ifelse(
is.na(n),
"<span class='stratlabel'>%s</span>",
"<span class='stratlabel'>%s<br><span class='stratn'>(N=%s; %s%%)</span></span>"
),
label, n, pct)
}
# New function to only print dichotomous "yes"
rndr <- function(x, ...) {
y <- render.default(x, ...)
if (is.logical(x)) y[2] else y
}
# if only wanting mean & SD
my.render.cont <- function(x) {
with(stats.apply.rounding(stats.default(x), digits=2),
c("", "Mean (SD)"=sprintf("%s (&plusmn; %s)", MEAN, SD)))
}
@kyleGrealis
Copy link
Copy Markdown
Author

for use with Benjamin Rich's table1 package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment