Show Menu
Cheatography

The Tableby function Cheat Sheet (DRAFT) by

The tableby function of arsenal package to make medical summary statistics in R

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Simple Example

tab1 <- tableb­y(arm ~ sex + age, data=m­ock­study)
summar­y(tab1, text=TRUE) # text version
as.dat­a.f­ram­e(tab1) #Data frame version of table

Modifying Output

labels­(mo­cks­tudy) <- c(age = 'Age, yrs', sex = "­Gen­der­") #Add labels to dataframe
labels­(tab1) <- c(arm=­"­Tre­atment Assign­men­t", age="Ba­seline Age (yrs)") # or add labels to tableby object.

Change summary statistics globally

mycontrols <- tableb­y.c­ont­rol­(te­st=­FALSE, total=­FALSE,
numeri­c.t­est­="kw­t", cat.te­st=­"­chi­sq",
numeri­c.s­tat­s=c­("N", "­med­ian­", "­q1q­3"),
cat.st­ats­=c(­"­cou­ntp­ct"),
stats.l­ab­els­=li­st(­N='­Count', median­='M­edian', q1q3='­Q1,­Q3'))
tab2 <- tableb­y(arm ~ sex + age, data=m­ock­study, contro­l=m­yco­ntrols)
summar­y(tab2)

Change these settings directly in the tableby call

tab3 <- tableb­y(arm ~ sex + age, data=m­ock­study, test=F­ALSE, total=­FALSE,
numeri­c.s­tat­s=c­("me­dia­n","q­1q3­"), numeri­c.t­est­="kw­t")
summar­y(tab3)

Change summary statistics within the formula

tab.test <- tableb­y(arm ~ kwt(age) + anova(bmi) + notest­(ast), data=m­ock­study)
tests(­tab.test)
summar­y(t­ab.t­est)

tab.test <- tableb­y(arm ~ kwt(ast, "­Nmi­ss2­"­,"me­dia­n") + anova(age, "­N","m­ean­") +
notest­(bmi, "­Nmi­ss",­"­med­ian­"), data=m­ock­study)
summar­y(t­ab.t­est)

Contro­lling Options for Catego­rical Tests

set.se­ed(100)
tab.catsim <- tableb­y(arm ~ sex + race, cat.te­st=­"­fe", simula­te.p.v­alu­e=TRUE, B=500,
data=m­ock­study) # simula­tions for these with 500 replicates for the Fisher’s test (fe).
tests(­tab.ca­tsim)

cat.co­rrect <- tableb­y(arm ~ sex + race, cat.te­st=­"­chi­sq", subset = !grepl­("^F­", arm),
data=m­ock­study) #use subset to ignore one of the three treatment arms.
tests(­cat.co­rrect)

Word version of the table

write2­doc­(tab1, "­tab­1.d­oc")

Summarize without a group/by variable

tab.noby <- tableby(~ bmi + sex + age, data=m­ock­study)
summar­y(t­ab.n­oby)

Display footnotes which test was used

summar­y(t­ab.t­est, pfootn­ote­=TRUE)

Summarize an ordered factor

mockst­udy­$ag­e.o­rdnew <- ordere­d(c­("a",­NA,­as.c­ha­rac­ter­(mo­cks­tud­y$a­ge.o­rd­[-(­1:2­)])))
table(­moc­kst­udy­$ag­e.ord, mockst­udy­$sex)

Summarize a survival variable

survfi­t(S­urv­(fu.time, fu.sta­t)~sex, data=m­ock­study) # The default is to show the median survival
survdi­ff(­Sur­v(f­u.time, fu.sta­t)~sex, data=m­ock­study)
summar­y(t­abl­eby(sex ~ Surv(f­u.time, fu.stat), data=m­ock­study))
 

Subsetting

mytab <- tableb­y(arm ~ sex + alk.phos + age, data=m­ock­study)
mytab2 <- mytab[­c('­age­','­sex­','­alk.ph­os')]
summar­y(m­ytab2)
summar­y(s­ort­(mytab, decreasing = TRUE))
summar­y(m­yta­b[mytab < 0.5])

Merge two tableby objects together

demogr­aphics
tab1 <- tableb­y(arm ~ sex + age, data=m­ock­study,
contro­l=t­abl­eby.co­ntr­ol(­num­eri­c.s­tat­s=c­("Nm­iss­"­,"me­ans­d"), total=­FALSE))
lab data
tab2 <- tableb­y(arm ~ hgb + alk.phos, data=m­ock­study,
contro­l=t­abl­eby.co­ntr­ol(­num­eri­c.s­tat­s=c­("Nm­iss­"­,"me­dia­n","q­1q3­"),
numeri­c.t­est­="kw­t", total=­FALSE))
tab12 <- merge(­tab1, tab2)
class(­tab12)

Modify how missing values are displayed

Show how many subjects have each variable (non-m­issing)
summar­y(t­abl­eby(sex ~ ast + age, data=m­ock­study,
contro­l=t­abl­eby.co­ntr­ol(­num­eri­c.s­tat­s=c­("N",­"­med­ian­"), total=­FAL­SE)))

Always list the number of missing values
summar­y(t­abl­eby(sex ~ ast + age, data=m­ock­study,
contro­l=t­abl­eby.co­ntr­ol(­num­eri­c.s­tat­s=c­("Nm­iss­2","m­edi­an"), total=­FAL­SE)))

Only show the missing values if there are some (default)
summar­y(t­abl­eby(sex ~ ast + age, data=m­ock­study,
contro­l=t­abl­eby.co­ntr­ol(­num­eri­c.s­tat­s=c­("Nm­iss­"­,"me­an")­,to­tal­=FA­LSE)))

Don't show N at all
summar­y(t­abl­eby(sex ~ ast + age, data=m­ock­study,
contro­l=t­abl­eby.co­ntr­ol(­num­eri­c.s­tat­s=c­("me­an")­,to­tal­=FA­LSE)))

Modify the number of digits used

summar­y(t­abl­eby(arm ~ sex + age + fu.time, data=m­ock­study), digits=4, digits.p=2, digits.pct=1)

summar­y(t­abl­eby(arm ~ chisq(sex, digits.pct=1) + anova(age, digits=4) +
anova(­fu.t­ime, digits = 1), data=m­ock­study))

Tabulate data by a non-test group (strata)

summar­y(t­abl­eby­(li­st(sex, ps) ~ age + bmi, strata = arm, data = mockst­udy))

Summary statistics

The default summary statis­tics, by varible type, are:
numeri­c.s­tats: Continuous variables will show by default Nmiss, meansd, range
cat.stats: Catego­rical and factor variables will show by default Nmiss, countpct
ordere­d.s­tats: Ordered factors will show by default Nmiss, countpct
surv.s­tats: Survival variables will show by default Nmiss, Nevents, medsurv
date.s­tats: Date variables will show by default Nmiss, median, range

There are a number of extra functions defined specifi­cally for the tableby function.
N, Nmiss, Nmiss2, meansd, count, countN, countpct, countr­owpct, q1q3, iqr

Testing options
anova, kwt, chisq, fe