options(width = 100) ge330 <- getRversion() >= "3.3.0"
One of the most common tables in medical literature includes summary statistics for a set of variables,
often stratified by some group (e.g. treatment arm). Locally at Mayo, the SAS macros %table
and %summary
were
written to create summary tables with a single call. With the increasing interest in R, we have developed
the function tableby
to create similar tables within the R environment.
In developing the tableby()
function, the goal was to bring the best features of these macros into an R function.
However, the task was not simply to duplicate all the functionality, but rather to make use of R's strengths
(modeling, method dispersion, flexibility in function definition and output format) and make a tool that fits
the needs of R users. Additionally, the results needed to fit within the general reproducible research framework
so the tables could be displayed within an R markdown report.
This report provides step-by-step directions for using the functions associated with tableby()
.
All functions presented here are available within the arsenal
package. An assumption is made that users
are somewhat familiar with R Markdown documents. For those who are new to the topic, a good initial resource
is available at rmarkdown.rstudio.com.
The first step when using the tableby
function is to load the arsenal
package. All the examples in this report use
a dataset called mockstudy
made available by Paul Novotny which includes a variety of types of variables (character,
numeric, factor, ordered factor, survival) to use as examples.
library(arsenal) require(knitr) require(survival) data(mockstudy) ##load data dim(mockstudy) ##look at how many subjects and variables are in the dataset # help(mockstudy) ##learn more about the dataset and variables str(mockstudy) ##quick look at the data
To create a simple table stratified by treatment arm, use a formula statement to specify the variables that you want summarized. The example below uses age (a continuous variable) and sex (a factor).
tab1 <- tableby(arm ~ sex + age, data=mockstudy)
If you want to take a quick look at the table, you can use summary()
on your tableby object and the table
will print out as text in your R console window. If you use summary()
without any options you will see a
number of $\ $ statements which translates to "space" in HTML.
If you want a nicer version in your console window then add the text=TRUE
option.
summary(tab1, text=TRUE)
In order for the report to look nice within an R markdown (knitr) report, you just need to specify
results="asis"
when creating the R chunk. This changes the layout slightly (compresses it) and bolds the variable names.
summary(tab1)
If you want a data.frame version, simply use as.data.frame
.
as.data.frame(tab1)
## base R frequency example tmp <- table(Gender=mockstudy$sex, "Study Arm"=mockstudy$arm) tmp # Note: The continuity correction is applied by default in R (not used in %table) chisq.test(tmp) ## base R numeric summary example tapply(mockstudy$age, mockstudy$arm, summary) summary(aov(age ~ arm, data=mockstudy))
In the above example, age is shown with a label (Age in Years), but sex is listed "as is" with lower case letters. This is because the data was created in SAS and in the SAS dataset, age had a label but sex did not. The label is stored as an attribute within R.
## Look at one variable's label attr(mockstudy$age,'label') ## See all the variables with a label unlist(lapply(mockstudy,'attr','label')) # Can also use labels(mockstudy)
If you want to add labels to other variables, there are a couple of options. First, you could add labels to the variables in your dataset.
attr(mockstudy$sex,'label') <- 'Gender' tab1 <- tableby(arm ~ sex + age, data=mockstudy) summary(tab1)
You can also use the built-in data.frame
method for labels<-
:
labels(mockstudy) <- c(age = 'Age, yrs', sex = "Gender") tab1 <- tableby(arm ~ sex + age, data=mockstudy) summary(tab1)
Another option is to add labels after you have created the table
mylabels <- list(sex = "SEX", age = "Age, yrs") summary(tab1, labelTranslations = mylabels)
Alternatively, you can check the variable labels and manipulate them with a function called labels
, which works on the tableby
object.
labels(tab1) labels(tab1) <- c(arm="Treatment Assignment", age="Baseline Age (yrs)") labels(tab1)
summary(tab1)
Currently the default behavior is to summarize continuous variables with: Number of missing values,
Mean (SD), 25th - 75th quantiles, and Minimum-Maximum values with an ANOVA (t-test with equal variances) p-value.
For categorical variables the default is to show: Number of missing values and count (column percent) with a
chi-square p-value. This behavior can be modified using the tableby.control function. In fact, you can save
your standard settings and use that for future tables. Note that test=FALSE
and total=FALSE
results in the
total column and p-value column not being printed.
mycontrols <- tableby.control(test=FALSE, total=FALSE, numeric.test="kwt", cat.test="chisq", numeric.stats=c("N", "median", "q1q3"), cat.stats=c("countpct"), stats.labels=list(N='Count', median='Median', q1q3='Q1,Q3')) tab2 <- tableby(arm ~ sex + age, data=mockstudy, control=mycontrols) summary(tab2)
You can also change these settings directly in the tableby call.
tab3 <- tableby(arm ~ sex + age, data=mockstudy, test=FALSE, total=FALSE, numeric.stats=c("median","q1q3"), numeric.test="kwt") summary(tab3)
In addition to modifying summary options globally, it is possible to modify the test and summary statistics for specific variables within the formula statement. For example, both the kwt (Kruskal-Wallis rank-based) and anova (asymptotic analysis of variance) tests apply to numeric variables, and we can use one for the variable "age", another for the variable "bmi", and no test for the variable "ast". A list of all the options is shown at the end of the vignette.
The tests
function can do a quick check on what tests were performed on each variable in tableby.
tab.test <- tableby(arm ~ kwt(age) + anova(bmi) + notest(ast), data=mockstudy) tests(tab.test)
summary(tab.test)
Summary statistics for any individual variable can also be modified, but it must be done as secondary arguments to the test function. The function names must be strings that are functions already written for tableby, built-in R functions like mean and range, or user-defined functions.
tab.test <- tableby(arm ~ kwt(ast, "Nmiss2","median") + anova(age, "N","mean") + notest(bmi, "Nmiss","median"), data=mockstudy) summary(tab.test)
These can also be passed to the stats=
argument.
tab.test <- tableby(arm ~ kwt(ast, stats = c("Nmiss2", "median")) + anova(age, stats = c("N","mean")) + notest(bmi, stats = c("Nmiss","median")), data=mockstudy) summary(tab.test)
The formal tests for categorical variables against the levels of the by variable, chisq and fe, have options to simulate p-values. We show how to turn on the simulations for these with 500 replicates for the Fisher's test (fe).
set.seed(100) tab.catsim <- tableby(arm ~ sex + race, cat.test="fe", simulate.p.value=TRUE, B=500, data=mockstudy) tests(tab.catsim)
The chi-square test on 2x2 tables applies Yates' continuity correction by default, so we provide an option to turn off the correction. We show the results with and without the correction that is applied to treatment arm by sex, if we use subset to ignore one of the three treatment arms.
cat.correct <- tableby(arm ~ sex + race, cat.test="chisq", subset = !grepl("^F", arm), data=mockstudy) tests(cat.correct) cat.nocorrect <- tableby(arm ~ sex + race, cat.test="chisq", subset = !grepl("^F", arm), chisq.correct=FALSE, data=mockstudy) tests(cat.nocorrect)
You can easily create Word versions of tableby
output via an Rmarkdown report and the default options will give you a reasonable table in Word - just select the "Knit Word" option in RStudio.
The functionality listed in this next paragraph is coming soon but needs an upgraded version of RStudio
If you want to modify fonts used for the table, then you'll need to add an extra line to your header at the beginning of your file.
You can take the WordStylesReference01.docx
file and modify the fonts (storing the format preferences in your project directory).
To see how this works, run your report once using WordStylesReference01.docx and then WordStylesReference02.docx.
output: word_document reference_docx: /projects/bsi/gentools/R/lib320/arsenal/doc/WordStylesReference01.docx
For more information on changing the look/feel of your Word document, see the Rmarkdown documentation website.
Here are multiple examples showing how to use some of the different options.
tab.noby <- tableby(~ bmi + sex + age, data=mockstudy) summary(tab.noby)
summary(tab.test, pfootnote=TRUE)
When comparing groups of ordered data there are a couple of options. The default uses a general independence test available from the coin
package.
For two-group comparisons, this is essentially the Armitage trend test. The other option is to specify the Kruskal Wallis test.
The example below shows both options.
mockstudy$age.ordnew <- ordered(c("a",NA,as.character(mockstudy$age.ord[-(1:2)]))) table(mockstudy$age.ord, mockstudy$sex) table(mockstudy$age.ordnew, mockstudy$sex) class(mockstudy$age.ord)
summary(tableby(sex ~ age.ordnew, data = mockstudy), pfootnote = TRUE) summary(tableby(sex ~ age.ord, data = mockstudy), pfootnote = TRUE)
First look at the information that is presented by the survfit()
function, then see how the same results can be seen with tableby.
The default is to show the median survival (time at which the probability of survival = 50%).
survfit(Surv(fu.time, fu.stat)~sex, data=mockstudy) survdiff(Surv(fu.time, fu.stat)~sex, data=mockstudy)
summary(tableby(sex ~ Surv(fu.time, fu.stat), data=mockstudy))
It is also possible to obtain summaries of the % survival at certain time points (say the probability of surviving 1-year).
summary(survfit(Surv(fu.time/365.25, fu.stat)~sex, data=mockstudy), times=1:5)
summary(tableby(sex ~ Surv(fu.time/365.25, fu.stat), data=mockstudy, times=1:3, surv.stats=c("NeventsSurv"))) summary(tableby(sex ~ Surv(fu.time/365.25, fu.stat), data=mockstudy, times=1:5, surv.stats=c("NriskSurv"))) summary(tableby(sex ~ Surv(fu.time/365.25, fu.stat), data=mockstudy, surv.stats="medSurvCI", survconf.type='log-log'), digits=2) summary(tableby(sex ~ Surv(fu.time/365.25, fu.stat), data=mockstudy, surv.stats="medSurvQuant"), digits=2)
Date variables by default are summarized with the number of missing values, the median, and the range. For example purposes we've created a random date. Missing values are introduced for impossible February dates.
set.seed(100) N <- nrow(mockstudy) mockstudy$dtentry <- mdy.Date(month=sample(1:12,N,replace=T), day=sample(1:29,N,replace=T), year=sample(2005:2009,N,replace=T)) summary(tableby(sex ~ dtentry, data=mockstudy))
Often one wants to summarize a number of variables. Instead of typing by hand each individual variable,
an alternative approach is to create a formula using the paste
command with the collapse="+"
option.
## create a vector specifying the variable names myvars <- names(mockstudy) ## select the 8th through the last variables ## paste them together, separated by the + sign RHS <- paste(myvars[8:10], collapse="+") RHS ## create a formula using the as.formula function as.formula(paste('arm ~ ', RHS)) ## use the formula in the tableby function summary(tableby(as.formula(paste('arm ~', RHS)), data=mockstudy))
These steps can also be done using the formulize
function.
## The formulize function does the paste and as.formula steps tmp <- formulize('arm',myvars[8:10]) tmp ## More complex formulas could also be written using formulize tmp2 <- formulize('arm',c('ps','hgb^2','bmi')) ## use the formula in the tableby function summary(tableby(tmp, data=mockstudy))
To change summary statistics or statistical tests en masse, consider using paste0()
together with formulize()
:
varlist1 <- c('age','sex','hgb') varlist2 <- paste0("anova(", c('bmi','alk.phos','ast'), ", 'meansd')") summary(tableby(formulize("arm", c(varlist1, varlist2)), data = mockstudy, numeric.test = "kwt"), pfootnote = TRUE)
Here are two ways to get the same result (limit the analysis to subjects age>5 and in the F: FOLFOX treatment group).
mockstudy
. This example also selects a subset of variables.
The tableby
function is then applied to this subsetted data.newdata <- subset(mockstudy, subset=age>50 & arm=='F: FOLFOX', select = c(sex,ps:bmi)) dim(mockstudy) table(mockstudy$arm) dim(newdata) names(newdata)
summary(tableby(sex ~ ., data=newdata))
tableby
to subset the data.summary(tableby(sex ~ ps + hgb + bmi, subset=age>50 & arm=="F: FOLFOX", data=mockstudy))
## create a variable combining the levels of mdquality.s and sex with(mockstudy, table(interaction(mdquality.s,sex)))
summary(tableby(arm ~ interaction(mdquality.s,sex), data=mockstudy))
## create a new grouping variable with combined levels of arm and sex summary(tableby(interaction(mdquality.s, sex) ~ age + bmi, data=mockstudy, subset=arm=="F: FOLFOX"))
Certain transformations need to be surrounded by I()
so that R knows to treat it as a variable transformation and
not some special model feature. If the transformation includes any of the symbols / - + ^ *
then surround the new variable by I()
.
trans <- tableby(arm ~ I(age/10) + log(bmi) + factor(mdquality.s, levels=0:1, labels=c('N','Y')), data=mockstudy) summary(trans)
The labels for these variables aren't exactly what we'd like, so we can change modify those after the fact. Instead of typing out the very long variable names, you can modify specific labels by position.
labels(trans) labels(trans)[2:4] <- c('Age per 10 yrs', 'log(BMI)', 'MD Quality') labels(trans)
summary(trans)
Note that if we had not changed mdquality.s
to a factor, it would have been summarized as though it were a continuous variable.
class(mockstudy$mdquality.s) summary(tableby(arm~mdquality.s, data=mockstudy))
Another option would be to specify the test and summary statistics. In fact, if I had a set of variables coded 0/1 and that was all I was summarizing, then I could change the global option for continuous variables to use the chi-square test and show countpct.
summary(tableby(arm ~ chisq(mdquality.s, "Nmiss","countpct"), data=mockstudy))
mytab <- tableby(arm ~ sex + alk.phos + age, data=mockstudy) mytab2 <- mytab[c('age','sex','alk.phos')] summary(mytab2) summary(mytab[c('age','sex')], digits = 2) summary(mytab[c(3,1)], digits = 3) summary(sort(mytab, decreasing = TRUE)) summary(mytab[mytab < 0.5]) head(mytab, 1) # can also use tail() summary(tableby(list(arm, sex) ~ sex + alk.phos + age, data=mockstudy)[, "sex"]) summary(tableby(list(arm, sex) ~ sex + alk.phos + age, data=mockstudy)[, list(sex = "Female", arm = c("F: FOLFOX", "Total"))])
tableby
objects togetherIt is possible to combine two tableby objects so that they print out together. Overlapping by-variables will have their x-variables
concatenated, and (if all=TRUE
) non-overlapping by-variables will have their tables printed separately.
## demographics tab1 <- tableby(arm ~ sex + age, data=mockstudy, control=tableby.control(numeric.stats=c("Nmiss","meansd"), total=FALSE)) ## lab data tab2 <- tableby(arm ~ hgb + alk.phos, data=mockstudy, control=tableby.control(numeric.stats=c("Nmiss","median","q1q3"), numeric.test="kwt", total=FALSE)) tab12 <- merge(tab1, tab2) class(tab12) summary(tab12)
For tables with two different outcomes, consider the all=TRUE
argument:
summary(merge( tableby(sex ~ age, data = mockstudy), tableby(arm ~ bmi, data = mockstudy), all = TRUE ))
When creating a pdf the tables are automatically numbered and the title appears below the table. In Word and HTML, the titles appear un-numbered and above the table.
t1 <- tableby(arm ~ sex + age, data=mockstudy) summary(t1, title='Demographics')
With multiple left-hand sides, you can pass a vector or list to determine labels for each table:
summary(tableby(list(arm, sex) ~ age, data = mockstudy), title = c("arm table", "sex table"))
Depending on the report you are writing you have the following options:
Show how many subjects have each variable
Show how many subjects are missing each variable
Show how many subjects are missing each variable only if there are any missing values
Don't indicate missing values at all
## look at how many missing values there are for each variable apply(is.na(mockstudy),2,sum)
## Show how many subjects have each variable (non-missing) summary(tableby(sex ~ ast + age, data=mockstudy, control=tableby.control(numeric.stats=c("N","median"), total=FALSE))) ## Always list the number of missing values summary(tableby(sex ~ ast + age, data=mockstudy, control=tableby.control(numeric.stats=c("Nmiss2","median"), total=FALSE))) ## Only show the missing values if there are some (default) summary(tableby(sex ~ ast + age, data=mockstudy, control=tableby.control(numeric.stats=c("Nmiss","mean"),total=FALSE))) ## Don't show N at all summary(tableby(sex ~ ast + age, data=mockstudy, control=tableby.control(numeric.stats=c("mean"),total=FALSE)))
One might also consider the use of includeNA()
to include NAs in the counts and percents for categorical variables.
mockstudy$ps.cat <- factor(mockstudy$ps) attr(mockstudy$ps.cat, "label") <- "ps" summary(tableby(sex ~ includeNA(ps.cat), data = mockstudy, cat.stats = "countpct"))
Within tableby.control function there are 4 options for controlling the number of significant digits shown.
digits: controls the number of digits after the decimal place for continuous values
digits.count: controls the number of digits after the decimal point for counts
digits.pct: controls the number of digits after the decimal point for percents
digits.p: controls the number of digits after the decimal point for p-values
summary(tableby(arm ~ sex + age + fu.time, data=mockstudy), digits=4, digits.p=2, digits.pct=1)
All of these can be specified on a per-variable basis using the in-formula functions that specify which tests are run:
summary(tableby(arm ~ chisq(sex, digits.pct=1) + anova(age, digits=4) + anova(fu.time, digits = 1, digits.p = 6, format.p = FALSE), data=mockstudy))
For purposes of this example, the code below creates a trimmed mean function (trims 10%)
and use that to summarize the data. Note the use of the ...
which tells R to pass extra arguments on - this is required
for user-defined functions. In this case, na.rm=T
is passed to myfunc
. The weights argument is also required, even though
it isn't passed on to the internal function in this particular example.
trim10 <- function(x, weights=NULL, ...){ mean(x, trim=.1, ...) } summary(tableby(sex ~ hgb, data=mockstudy, control=tableby.control(numeric.stats=c("Nmiss","trim10"), numeric.test="kwt", stats.labels=list(Nmiss='Missing values', trim10="Trimmed Mean, 10%"))))
For statistics to be formatted appropriately, you may want to use as.tbstat()
.
For example, suppose you want to create a trimmed mean function that trims by both 5 and 10 percent.
The first example shows them separated by a comma; the second puts the 10% trimmed mean in brackets.
as.tbstat()
takes a fmt=
argument with a glue
string specification, where the current value is exposed as
x
and a formatted value (using tbfmt()
) is exposed as y
. See ?as.tbstat
for details.
trim510comma <- function(x, ...){ tmp <- c(mean(x, trim = 0.05, ...), mean(x, trim = 0.1, ...)) as.tbstat(tmp, fmt = "{y[1]}, {y[2]}") } trim510bracket <- function(x, ...){ tmp <- c(mean(x, trim = 0.05, ...), mean(x, trim = 0.1, ...)) as.tbstat(tmp, fmt = "{y[1]} [{y[2]}]") } summary(tableby(sex ~ hgb, data=mockstudy, numeric.stats=c("Nmiss", "trim510comma", "trim510bracket"), test = FALSE))
Or perhaps it's useful to put the amount of trimming in parentheses. Since it is a percent, we can flag it as such:
trim10pct <- function(x, ...){ tmp <- mean(x, trim = 0.05, ...) as.tbstat(c(tmp, 10), fmt = "{y[1]} ({y[2]}%)", which.pct = 2L) } summary(tableby(sex ~ hgb, data=mockstudy, numeric.stats=c("Nmiss", "trim10pct"), digits = 2, digits.pct = 0, test = FALSE))
Finally, if there's a pre-computed summary statistic, you can easily change the formatting, like in meanpmsd()
:
meanpmsd
For example:
dollarmean <- function(x, na.rm = TRUE, ...) { if(na.rm && allNA(x)) { as.tbstat(NA_real_) } else { out <- meansd(x, na.rm = na.rm, ...) attr(out, "fmt") <- "${y[1]}" out } } summary(tableby(sex ~ notest(age, "dollarmean", digits = 2), data = mockstudy))
When comparing groups, they are often unbalanced when it comes to nuisances such as age and sex.
The tableby
function allows you to create weighted summary statistics. If this option us used then p-values are not calculated (test=FALSE
).
##create fake group that is not balanced by age/sex set.seed(200) mockstudy$fake_arm <- ifelse(mockstudy$age>60 & mockstudy$sex=='Female',sample(c('A','B'),replace=T, prob=c(.2,.8)), sample(c('A','B'),replace=T, prob=c(.8,.4))) mockstudy$agegp <- cut(mockstudy$age, breaks=c(18,50,60,70,90), right=FALSE) ## create weights based on agegp and sex distribution tab1 <- with(mockstudy,table(agegp, sex)) tab2 <- with(mockstudy, table(agegp, sex, fake_arm)) tab2 gpwts <- rep(tab1, length(unique(mockstudy$fake_arm)))/tab2 gpwts[gpwts>50] <- 30 ## apply weights to subjects index <- with(mockstudy, cbind(as.numeric(agegp), as.numeric(sex), as.numeric(as.factor(fake_arm)))) mockstudy$wts <- gpwts[index] ## show weights by treatment arm group tapply(mockstudy$wts,mockstudy$fake_arm, summary)
orig <- tableby(fake_arm ~ age + sex + Surv(fu.time/365, fu.stat), data=mockstudy, test=FALSE) summary(orig, title='No Case Weights used') tab1 <- tableby(fake_arm ~ age + sex + Surv(fu.time/365, fu.stat), data=mockstudy, weights=wts) summary(tab1, title='Case Weights used')
When using weighted summary statistics, it is often desirable to then show a p-value from a model that corresponds to the weighted analysis. It is possible to add your own p-value and modify the column title for that new p-value. Another use for this would be to add standardized differences or confidence intervals instead of a p-value.
To add the p-value, you simply need to create a data frame and use the function modpval.tableby()
.
The first few columns in the data.frame are required: (1) the by-variable, (2) the strata value (if
the table has a strata term), (3) the x-variable, and (4) the new p-value (or test statistic).
Another optional column can be used to indicate what method was used to calculate the p-value.
If you specify use.pname=TRUE
then the column name indicating the p-value will be also be used in the tableby summary.
mypval <- data.frame( byvar = "fake_arm", variable = c('age','sex','Surv(fu.time/365, fu.stat)'), adj.pvalue = c(.953,.811,.01), method = c('Age/Sex adjusted model results') ) tab2 <- modpval.tableby(tab1, mypval, use.pname=TRUE) summary(tab2, title='Case Weights used, p-values added', pfootnote=TRUE)
If the cat.simplify
option is set to TRUE
, then only the second level of two-level
categorical varialbes is shown. In the example below, sex
has two levels,
and "Female" is the second level, hence only the counts and percents for Female are shown.
Similarly, "mdquality.s" was turned to a factor, and "1" is the second level, but since
there are missings, the table ignores cat.simplify
and displays all levels (since the
output can no longer be displayed on one line).
table2 <- tableby(arm~sex + factor(mdquality.s), data=mockstudy, cat.simplify=TRUE) summary(table2, labelTranslations=c(sex="Female", "factor(mdquality.s)"="MD Quality"))
Similarly, if numeric.simplify
is set to TRUE
, then any numerics which only have one
row of summary statistics are simplified into a single row. Note again that ast
has
missing values and so is not simplified to a single row.
summary(tableby(arm ~ age + ast, data = mockstudy, numeric.simplify=TRUE, numeric.stats=c("Nmiss", "meansd")))
The in-formula functions to change which tests are run can also be used to specify these options for each variable at a time.
summary(tableby(arm ~ anova(age, "meansd", numeric.simplify=TRUE) + chisq(sex, cat.simplify=TRUE), data = mockstudy))
The cat.simplify
and ord.simplify
argument also accept the special string "label"
, which appends the shown level's label to the overall label:
summary(tableby(arm ~ sex, cat.simplify = "label", data = mockstudy))
tableby
within an Sweave documentFor those users who wish to create tables within an Sweave document, the following code seems to work.
\documentclass{article} \usepackage{longtable} \usepackage{pdfpages} \begin{document} \section{Read in Data} <<echo=TRUE>>= require(arsenal) require(knitr) require(rmarkdown) data(mockstudy) tab1 <- tableby(arm~sex+age, data=mockstudy) @ \section{Convert Summary.Tableby to LaTeX} <<echo=TRUE, results='hide', message=FALSE>>= capture.output(summary(tab1), file="Test.md") ## Convert R Markdown Table to LaTeX render("Test.md", pdf_document(keep_tex=TRUE)) @ \includepdf{Test.pdf} \end{document}
tableby
object to a .CSV fileWhen looking at multiple variables it is sometimes useful to export the results to a csv file. The as.data.frame
function creates a data frame object that can be exported or further manipulated within R.
tab1 <- summary(tableby(arm~sex+age, data=mockstudy), text = NULL) as.data.frame(tab1) # write.csv(tab1, '/my/path/here/my_table.csv')
tableby
object to a separate Word or HTML file## write to an HTML document tab1 <- tableby(arm ~ sex + age, data=mockstudy) write2html(tab1, "~/trash.html") ## write to a Word document write2word(tab1, "~/trash.doc", title="My table in Word")
tableby
in R ShinyThe easiest way to output a tableby()
object in an R Shiny app is to use the tableOutput()
UI in combination with
the renderTable()
server function and as.data.frame(summary(tableby()))
:
# A standalone shiny app library(shiny) library(arsenal) data(mockstudy) shinyApp( ui = fluidPage(tableOutput("table")), server = function(input, output) { output$table <- renderTable({ as.data.frame(summary(tableby(sex ~ age, data = mockstudy), text = "html")) }, sanitize.text.function = function(x) x) } )
This can be especially powerful if you feed the selections from a selectInput(multiple = TRUE)
into formulize()
to make
the table dynamic!
tableby
in bookdownSince the backbone of tableby()
is knitr::kable()
, tables still render well in bookdown. However, print.summary.tableby()
doesn't use
the caption=
argument of kable()
, so some tables may not have a properly numbered caption. To fix this, use the method described
on the bookdown site to give the table a tag/ID.
summary(tableby(sex ~ age, data = mockstudy), title="(\\#tab:mytableby) Caption here")
tableby
for multiple p-valuesThe padjust()
function is a new S3 generic piggybacking off of p.adjust()
. It works on both tableby
and summary.tableby
objects:
tab <- summary(tableby(sex ~ age + fu.time + bmi + mdquality.s, data = mockstudy)) tab padjust(tab, method = "bonferroni")
You can now use list()
on the left-hand side of tableby()
to give multiple endpoints.
summary(tableby(list(sex, mdquality.s, ps) ~ age + bmi, data = mockstudy))
To avoid confusion about which table is which endpoint, you can set term.name=TRUE
in summary()
. This takes the labels
for each by-variable and puts them in the top-left of the table.
summary(tableby(list(sex, mdquality.s, ps) ~ age + bmi, data = mockstudy), term.name = TRUE)
You can also specify a second grouping variable that doesn't get tested (but instead separates results): a strata variable.
summary(tableby(list(sex, ps) ~ age + bmi, strata = arm, data = mockstudy))
The default summary statistics, by varible type, are:
numeric.stats
: Continuous variables will show by default Nmiss, meansd, range
cat.stats
: Categorical and factor variables will show by default Nmiss, countpct
ordered.stats
: Ordered factors will show by default Nmiss, countpct
surv.stats
: Survival variables will show by default Nmiss, Nevents, medsurv
date.stats
: Date variables will show by default Nmiss, median, range
There are a number of extra functions defined specifically for the tableby function.
N
: a count of the non-missing number of observations for a particular groupNpct
: a count of the non-missing number of observations and the percentage of the column total (missing + non-missing) in the format N (%)
Nrowpct
: a count of the non-missing number of observations and the row-percentage (of non-missings) in the format N (%)
Nmiss
: only show the count of the number of missing values if there are some missing valuesNmiss2
: always show a count of the number of missing values for a variable within each groupNmisspct
: show the count of the number of missing values and the percentage of the column total (missing+non-missing) if there are some missing valuesNmisspct2
: The same as Nmisspct
, but always show.meanse
: print the mean and standard error in the format mean(se)
meanCI
: print the mean and a (t) confidence intervalcount
: print the number of values in a categorycountN
: print the number of values in a category plus the total N for the group in the format N/Total
countpct
: print the number of values in a category plus the column-percentage in the format N (%)
pct
: print the column-percentagecountrowpct
: print the number of values in a category plus the row-percentage in the format N (%)
rowpct
: print the row-percentagecountcellpct
: print the number of values in a category plus the cell-percentage in the format N (%)
binomCI
: print the proportion in a category plus a binomial confidence interval.rowbinomCI
: print the row proportion in a category plus a binomial confidence interval.medianq1q3
: print the median, 25th, and 75th quantiles median (Q1, Q3)
q1q3
: print the 25th and 75th quantiles Q1, Q3
iqr
: print the inter-quartile range.medianrange
: print the median, minimum and maximum values median (minimum, maximum)
medianmad
: print the median and median absolute deviation (mad)Nevents
: print number of events for a survival object within each grouping levelmedSurv
: print the median survivalNeventsSurv
: print number of events and survival at given timesNriskSurv
: print the number still at risk and survival at given timesNrisk
: print the number still at risk at given timesmedTime
: print the median follow-up timesum
max
min
mean
sd
var
median
range
gmean
, gsd
, gmeansd
, gmeanCI
: geometric means, sds, and confidence intervals.The tests used to calculate p-values differ by the variable type, but can be specified explicitly in the formula statement or in the control function.
The following tests are accepted:
anova
: analysis of variance test; the default test for continuous variables. When
the grouping variable has two levels, it is equivalent to the two-sample t-test with equal variance.
kwt
: Kruskal-Wallis test, optional test for continuous
variables. When the grouping variable has two levels, it is equivalent to the Wilcoxon Rank Sum test.
wt
: An explicit Wilcoxcon test.
medtest
: Median test test, optional test for continuous variables.
chisq
: chi-square goodness of fit test or Pearson chi-squared test; the default for categorical
or factor variables
fe
: Fisher's exact test for categorical variables; optional
logrank
: log-rank test, the default test for time-to-event
variables
trend
: The independence_test
function from the coin
is used to test for trends. Whenthe grouping variable has two levels,
it is equivalent to the Armitage trend test. This is the default for ordered factors
stddiff
: perform standardized differences.
notest
: Don't perform a test.
tableby.control
settingsA quick way to see what arguments are possible to utilize in a function is to use the args()
command. Settings involving the number of digits can be set in tableby.control
or in summary.tableby
.
args(tableby.control)
summary.tableby
settingsThe summary.tableby
function has options that modify how the table appears (such as adding a title or modifying labels).
args(arsenal:::summary.tableby)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.