Learn R Programming

⚠️There's a newer version (0.8.0) of this package.Take me there.

crosstable

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 last version available on CRAN (v0.3.1)
install.packages("crosstable")

# Install development version on Github
devtools::install_github("DanChaltiel/crosstable", build_vignettes=TRUE)

# Install specific version (for reproducibility purpose)
devtools::install_github("DanChaltiel/crosstable@v0.3.1", build_vignettes=TRUE) #last tag
devtools::install_github("DanChaltiel/crosstable@8bc07e0", build_vignettes=TRUE) #last commit

Note that, for reproducibility purpose, an even better solution would be to use renv.

Getting help and giving feedback

You can find the whole documentation on the dedicated website.

Besides, you can also use the vignettes:

  • vignette("crosstable") for global use and parameterization
  • vignette("crosstable-selection") for variable selection
  • vignette("crosstable-report") for reporting with officer or Rmarkdown

If you miss any feature that you think would belong in {crosstable}, please fill a Feature Request issue.

In case of any installation problem, try the solutions proposed in this article or fill a Bug Report issue.

Overview

library(crosstable)
library(dplyr)
ct1 = crosstable(mtcars2, c(disp, vs), by=am, total="both", 
                 percent_pattern="{n} ({p_row}/{p_col})", percent_digits=0) %>%
  as_flextable()

With only a few arguments, you can select which column to describe (c(disp, vs)), define a grouping variable (by=am), set the percentage calculation in row/column (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("cy"), ends_with("at")), by=c(am, vs), label=FALSE,
                 funs=c(mean, quantile), funs_arg=list(probs=c(.25,.75), digits=3)) %>% 
  as_flextable(compact=TRUE)

Here, the variables were selected using tidyselect helpers and the summary functions mean and quantile were specified, along with argument probs for the 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 a longer 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:

  • variable selection with functions, e.g. is.numeric (link)
  • variable selection with mutating, e.g. sqrt(mpg) or Surv(time, event), using a formula interface (link)
  • automatic computation of statistical tests (link) and of effect sizes (link)
  • global options to avoid repeating arguments (link)
  • description of correlation, dates, and survival data (link)
  • auto-reporting with officer (link) or with Rmarkdown (link)

Acknowledgement

crosstable was initially based on the awesome package biostat2 written by David Hajage. Thanks David!

Copy Link

Version

Install

install.packages('crosstable')

Monthly Downloads

5,074

Version

0.3.2

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Dan Chaltiel

Last Published

November 27th, 2021

Functions in crosstable (0.3.2)

body_add_crosstable_footnote

Adds a standard footnote explaining the abbreviations used in a crosstable
body_add_crosstable

Add a crosstable to an officer document
apply_labels

Batch set variable labels
body_add_img2

Alternative to officer::body_add_img() which adds a units choice
body_add_crosstable_list

Add a list of crosstables
as_workbook

Converts a crosstable object into a formatted, savable openxlsx workbook.
body_add_gg2

Alternative to officer::body_add_gg() which uses ggplot syntax
N

Return the number of non NA observations
as_flextable.crosstable

Turns a crosstable object into a formatted flextable
as_gt.crosstable

Converts a crosstable object into a formatted gt table.
crosstable-package

crosstable: Crosstables for Descriptive Analyses
body_add_list

Add a list to an officer document
body_add_legend

Add a legend to a table or a figure
body_add_normal

Add a new paragraph with default style
body_add_title

Add a title to an officer document
effect_tabular

Effect measure for association between two categorical variables
compact

Generic function to compact a table (publication formatting)
crosstable_test_args

crosstable_peek_options

See which crosstable option is currently set.
body_replace_text_at_bkms

Replace text on several bookmarks at once
effect_survival

Effect measure for association between one censored variable and one categorical variable
crosstable

Easily describe datasets
crosstable_effect_args

effect_summary

Effect measure for association between one continuous and one categorical variable
docx_bookmarks2

List Word bookmarks, including the ones in header and footer
crosstable_options

Options for the package crosstable
reexports

Objects exported from other packages
plim

test_tabular_auto

test for contingency table
iris2

Modified iris dataset
write_and_open

Alternative to default officer print() function. Write the file and try to open it right away.
mtcars2

Modified mtcars dataset
rename_dataframe_with_labels

Rename every column of a dataframe with its label
format_fixed

Format numbers with the exact same number of decimals, including trailing zeros
remove_labels

Remove all label attributes.
generate_autofit_macro

Generate a macro file for autofitting
test_summarize_linear_contrasts

Test for linear trend across ordered factor with contrasts
get_label

Get label if wanted and available, or default (name) otherwise
test_survival_logrank

test for survival comparison
test_correlation_auto

test for correlation coefficients
confint_numeric

Confidence interval of a numeric vector
test_summarize_auto

test for mean comparison
import_labels

Import labels
display_effect

Default function to display the effect
set_label

Set the "label" attribute of an object
na

Return the number of NA observations
peek

Open a crosstable in a temporary document
display_test

Default function to display a test result
cross_summary

Summarize a numeric vector
summaryFunctions

Summary functions