crosstable

Package-License Lifecycle: maturing CRAN status Last Commit Build Status Codecov test coverage R build status Crosstable is a package centered on a single function, crosstable, which easily computes descriptive statistics on datasets. It can use the tidyverse syntax and is interfaced with the package officer to create automatized reports.

Installation

install.packages("devtools")
devtools::install_github("DanChaltiel/crosstable", build_vignettes=TRUE)

This will install the very last version, which should contains the fewest bugs and the most features.

However, for reproducibility purpose, you might want to install a specific version in your projects (even better would be to use renv):

devtools::install_github("DanChaltiel/crosstable@v0.2.1", build_vignettes=TRUE) #last version
devtools::install_github("DanChaltiel/crosstable@0ce391e", build_vignettes=TRUE) #last commit

In case of any installation problem, try the solutions proposed in this article or fill an Issue.

Getting help

You can find the whole documentation on the the dedicated website.

Otherwise, you can also use the vignettes:

Overview

library(crosstable)
library(dplyr)
ct1 = crosstable(mtcars2, c(disp, vs), by=am, margin=c("row", "col"), total="both") %>%
  as_flextable()

crosstable1

With a few arguments, you can select which column to describe (disp, vs), define a grouping variable (by=am), set the percentage calculation (margin) and ask for totals (total).

mtcars2 is a dataset which has labels, so they are displayed instead of the variable name (see here for how to add some).

crosstable is returning a plain R object (data.frame), but using as_flextable allows to output a beautiful HTML table that can be exported to Word with a few more lines of code (see here to learn how).

Here is a more advanced example:

ct2 = crosstable(mtcars2, c(starts_with("c"), ends_with("t")), by=vs, label=FALSE,
                 funs=c(mean, quantile), funs_arg=list(probs=c(.25,.75), digits=3)) %>% 
  as_flextable(compact=TRUE)

crosstable2

Here, the variables were selected using tidyselect helpers and the summary functions mean and quantile were specified, with arguments for this latter. Using label=FALSE allowed to see which variables were selected but it is best to keep the labels in the final table. In as_flextable(), the compact=TRUE option yields an alternative output, which may be more suited in some contexts, for instance for publication.

More

There are lots of other features you can learn about on the website https://danchaltiel.github.io/crosstable/, for instance:

Acknowledgement

crosstable is a rewrite of the awesome package biostat2 written by David Hajage. The user interface is quite different but the concept is the same.

Thanks David!