\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{appleacid (appleacid)} \pdfinfo{ /Title (the-tableby-function.pdf) /Creator (Cheatography) /Author (appleacid (appleacid)) /Subject (The Tableby function Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{The Tableby function Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{appleacid (appleacid)} via \textcolor{DarkBackground}{\uline{cheatography.com/119008/cs/22106/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}appleacid (appleacid) \\ \uline{cheatography.com/appleacid} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 8th April, 2020.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Simple Example}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{tab1 \textless{}- tableby(arm \textasciitilde{} sex + age, data=mockstudy) \newline % Row Count 1 (+ 1) summary(tab1, text=TRUE) \# text version \newline % Row Count 2 (+ 1) as.data.frame(tab1) \#Data frame version of table% Row Count 3 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Modifying Output}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{labels(mockstudy) \textless{}- c(age = 'Age, yrs', sex = "Gender") \#Add labels to dataframe \newline % Row Count 2 (+ 2) labels(tab1) \textless{}- c(arm="Treatment Assignment", age="Baseline Age (yrs)") \# or add labels to tableby object.% Row Count 5 (+ 3) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Change summary statistics globally}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{mycontrols \textless{}- \seqsplit{tableby.control(test=FALSE}, total=FALSE, \newline % Row Count 2 (+ 2) numeric.test="kwt", cat.test="chisq", \newline % Row Count 4 (+ 2) numeric.stats=c("N", "median", "q1q3"), \newline % Row Count 6 (+ 2) cat.stats=c("countpct"), \newline % Row Count 8 (+ 2) \seqsplit{stats.labels=list(N='Count'}, median='Median', q1q3='Q1,Q3')) \newline % Row Count 10 (+ 2) tab2 \textless{}- tableby(arm \textasciitilde{} sex + age, data=mockstudy, control=mycontrols) \newline % Row Count 12 (+ 2) summary(tab2)% Row Count 13 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Change these settings directly in the tableby call}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{tab3 \textless{}- tableby(arm \textasciitilde{} sex + age, data=mockstudy, test=FALSE, total=FALSE, \newline % Row Count 2 (+ 2) numeric.stats=c("median","q1q3"), numeric.test="kwt") \newline % Row Count 4 (+ 2) summary(tab3)% Row Count 5 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Change summary statistics within the formula}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{tab.test \textless{}- tableby(arm \textasciitilde{} kwt(age) + anova(bmi) + notest(ast), data=mockstudy) \newline % Row Count 2 (+ 2) tests(tab.test) \newline % Row Count 3 (+ 1) summary(tab.test) \newline % Row Count 4 (+ 1) tab.test \textless{}- tableby(arm \textasciitilde{} kwt(ast, "Nmiss2","median") + anova(age, "N","mean") + \newline % Row Count 6 (+ 2) notest(bmi, "Nmiss","median"), data=mockstudy) \newline % Row Count 8 (+ 2) summary(tab.test)% Row Count 9 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Controlling Options for Categorical Tests}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{set.seed(100) \newline % Row Count 1 (+ 1) tab.catsim \textless{}- tableby(arm \textasciitilde{} sex + race, cat.test="fe", simulate.p.value=TRUE, B=500, \newline % Row Count 3 (+ 2) data=mockstudy) \# simulations for these with 500 replicates for the Fisher's test (fe). \newline % Row Count 5 (+ 2) tests(tab.catsim) \newline % Row Count 6 (+ 1) cat.correct \textless{}- tableby(arm \textasciitilde{} sex + race, cat.test="chisq", subset = !grepl("\textasciicircum{}F", arm), \newline % Row Count 8 (+ 2) data=mockstudy) \#use subset to ignore one of the three treatment arms. \newline % Row Count 10 (+ 2) tests(cat.correct)% Row Count 11 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Word version of the table}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{write2doc(tab1, "tab1.doc")% Row Count 1 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Summarize without a group/by variable}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{tab.noby \textless{}- tableby(\textasciitilde{} bmi + sex + age, data=mockstudy) \newline % Row Count 2 (+ 2) summary(tab.noby)% Row Count 3 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Display footnotes which test was used}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{summary(tab.test, pfootnote=TRUE)% Row Count 1 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Summarize an ordered factor}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{mockstudy\$age.ordnew \textless{}- ordered(c("a",NA,as.character(mockstudy\$age.ord{[}-(1:2){]}))) \newline % Row Count 2 (+ 2) table(mockstudy\$age.ord, mockstudy\$sex)% Row Count 3 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Summarize a survival variable}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{survfit(Surv(fu.time, fu.stat)\textasciitilde{}sex, data=mockstudy) \# The default is to show the median survival \newline % Row Count 2 (+ 2) survdiff(Surv(fu.time, fu.stat)\textasciitilde{}sex, data=mockstudy) \newline % Row Count 4 (+ 2) summary(tableby(sex \textasciitilde{} Surv(fu.time, fu.stat), data=mockstudy))% Row Count 6 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Subsetting}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{mytab \textless{}- tableby(arm \textasciitilde{} sex + alk.phos + age, data=mockstudy) \newline % Row Count 2 (+ 2) mytab2 \textless{}- mytab{[}c('age','sex','alk.phos'){]} \newline % Row Count 3 (+ 1) summary(mytab2) \newline % Row Count 4 (+ 1) summary(sort(mytab, decreasing = TRUE)) \newline % Row Count 5 (+ 1) summary(mytab{[}mytab \textless{} 0.5{]})% Row Count 6 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Merge two tableby objects together}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{demographics}} \newline % Row Count 1 (+ 1) tab1 \textless{}- tableby(arm \textasciitilde{} sex + age, data=mockstudy, \newline % Row Count 2 (+ 1) \seqsplit{control=tableby.control(numeric.stats=c("Nmiss"},"meansd"), total=FALSE)) \newline % Row Count 4 (+ 2) {\bf{lab data}} \newline % Row Count 5 (+ 1) tab2 \textless{}- tableby(arm \textasciitilde{} hgb + alk.phos, data=mockstudy, \newline % Row Count 7 (+ 2) \seqsplit{control=tableby.control(numeric.stats=c("Nmiss"},"median","q1q3"), \newline % Row Count 9 (+ 2) numeric.test="kwt", total=FALSE)) \newline % Row Count 10 (+ 1) tab12 \textless{}- merge(tab1, tab2) \newline % Row Count 11 (+ 1) class(tab12)% Row Count 12 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Modify how missing values are displayed}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Show how many subjects have each variable (non-missing)}} \newline % Row Count 2 (+ 2) summary(tableby(sex \textasciitilde{} ast + age, data=mockstudy, \newline % Row Count 3 (+ 1) \seqsplit{control=tableby.control(numeric.stats=c("N"},"median"), total=FALSE))) \newline % Row Count 5 (+ 2) {\bf{ Always list the number of missing values}} \newline % Row Count 6 (+ 1) summary(tableby(sex \textasciitilde{} ast + age, data=mockstudy, \newline % Row Count 7 (+ 1) \seqsplit{control=tableby.control(numeric.stats=c("Nmiss2"},"median"), total=FALSE))) \newline % Row Count 9 (+ 2) {\bf{ Only show the missing values if there are some (default)}} \newline % Row Count 11 (+ 2) summary(tableby(sex \textasciitilde{} ast + age, data=mockstudy, \newline % Row Count 12 (+ 1) \seqsplit{control=tableby.control(numeric.stats=c("Nmiss"},"mean"),total=FALSE))) \newline % Row Count 14 (+ 2) {\bf{ Don't show N at all}} \newline % Row Count 15 (+ 1) summary(tableby(sex \textasciitilde{} ast + age, data=mockstudy, \newline % Row Count 16 (+ 1) \seqsplit{control=tableby.control(numeric.stats=c("mean")},total=FALSE)))% Row Count 18 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Modify the number of digits used}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{summary(tableby(arm \textasciitilde{} sex + age + fu.time, data=mockstudy), digits=4, digits.p=2, digits.pct=1) \newline % Row Count 2 (+ 2) summary(tableby(arm \textasciitilde{} chisq(sex, digits.pct=1) + anova(age, digits=4) + \newline % Row Count 4 (+ 2) anova(fu.time, digits = 1), data=mockstudy))% Row Count 6 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Tabulate data by a non-test group (strata)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{summary(tableby(list(sex, ps) \textasciitilde{} age + bmi, strata = arm, data = mockstudy))% Row Count 2 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Summary statistics}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{The default summary statistics, by varible type, are:}} \newline % Row Count 2 (+ 2) numeric.stats: Continuous variables will show by default Nmiss, meansd, range \newline % Row Count 4 (+ 2) cat.stats: Categorical and factor variables will show by default Nmiss, countpct \newline % Row Count 6 (+ 2) ordered.stats: Ordered factors will show by default Nmiss, countpct \newline % Row Count 8 (+ 2) surv.stats: Survival variables will show by default Nmiss, Nevents, medsurv \newline % Row Count 10 (+ 2) date.stats: Date variables will show by default Nmiss, median, range \newline % Row Count 12 (+ 2) {\bf{There are a number of extra functions defined specifically for the tableby function.}} \newline % Row Count 14 (+ 2) N, Nmiss, Nmiss2, meansd, count, countN, countpct, countrowpct, q1q3, iqr \newline % Row Count 16 (+ 2) {\bf{Testing options}} \newline % Row Count 17 (+ 1) anova, kwt, chisq, fe% Row Count 18 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}