Learn R Programming

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
install.packages("crosstable")

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

# Install specific commit or tagged version (for reproducibility purpose)
devtools::install_github("DanChaltiel/crosstable@ee012f6", build_vignettes=TRUE)
devtools::install_github("DanChaltiel/crosstable@0.6.1", build_vignettes=TRUE)

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

Overview

Here are 2 examples to try and show you the main features of crosstable. See the documentation website for more.

Example #1

Dear crosstable, using the mtcars2 dataset, please describe columns disp and vs depending on the levels of column am, with totals in both rows and columns, and with proportions formatted with group size, percent on row and percent on column, with no decimals.

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

With only a few arguments, we did select which column to describe (c(disp, vs)), define a grouping variable (by=am), set the percentage calculation in row/column (percent_pattern=), and ask for totals (total=).

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

As crosstable() is returning a data.frame, we use as_flextable() to output a beautiful HTML table. This one can even be exported to MS Word with a few more lines of code (see here to learn how).

Example #2

Here is a more advanced example.

Dear crosstable, using the mtcars2 dataset again, please describe all columns whose name starts with "cy" and those whose name ends with "at", depending on the levels of both columns am and vs, without considering labels, applying mean() and quantile() as summary function, with probs 25% and 75% defined for this latter function, and with 3 decimals for numeric variables:

ct2 = crosstable(mtcars2, c(starts_with("cy"), ends_with("at")), by=c(am, vs), 
                 label=FALSE, num_digits=3, funs=c(mean, quantile), 
                 funs_arg=list(probs=c(.25,.75))) %>% 
  as_flextable(compact=TRUE, header_show_n=1:2)
ct2

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), and header_show_n=1:2 adds the group sizes for both rows of the header.

Documentation

You can find the whole documentation on the dedicated website:

  • vignette("crosstable") for a first step-by-step guide on how to use crosstable (link)
  • vignette("crosstable-report") for more on creating MS Word reports using either {officer} or Rmarkdown (link)
  • vignette("pertent_pattern") for more on how to use percent_pattern (link)
  • vignette("crosstable-selection") for more on variable selection (link), although you should better read https://tidyselect.r-lib.org/articles/syntax.html.

There are lots of other features you can learn about there, for instance (non-exhaustive list):

  • description of correlation, dates, and survival data (link)
  • variable selection with functions, e.g. is.numeric (link)
  • formula interface, allowing to describe more mutated columns, e.g. sqrt(mpg) or Surv(time, event) (link)
  • automatic computation of statistical tests (link) and of effect sizes (link)
  • global options to avoid repeating arguments (link)

Getting help and giving feedback

If you have a question about how to use crosstable, please ask on StackOverflow with the tag crosstable. You can @DanChaltiel in a comment if you are struggling to get answers. Don't forget to add a minimal reproducible example to your question, ideally using the reprex package.

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

If you encounter an unexpected error while using crosstable, please fill a Bug Report issue. In case of any installation problem, try the solutions proposed in this article first.

Acknowledgement

In its earliest development phase, crosstable was based on the awesome package biostat2 written by David Hajage. Thanks David!

Copy Link

Version

Install

install.packages('crosstable')

Monthly Downloads

6,000

Version

0.8.0

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Dan Chaltiel

Last Published

September 30th, 2024

Functions in crosstable (0.8.0)

body_replace_text_at_bkms

Replace text on several bookmarks at once
crosstable_options

Options for the package crosstable
generate_autofit_macro

Generate a macro file for autofitting
effect_survival

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

Effect measure for association between one continuous and one categorical variable
crosstable-package

crosstable: Crosstables for Descriptive Analyses
get_label

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

Generic function to compact a table (publication formatting)
display_effect

Default function to display the effect
display_test

Default function to display a test result
confint_numeric

Confidence interval of a numeric vector
docx_bookmarks2

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

Remove missing values
body_add_normal

Add a new paragraph with default style
crosstable_effect_args

Default arguments for calculating and displaying effects in crosstable()
crosstable

Easily describe datasets
mtcars2

Modified mtcars dataset
na

Return the number of NA observations
iris2

Modified iris dataset
is.crosstable

Test if an object is a crosstable
peek

Open a crosstable in a temporary document
rename_with_labels

Rename every column of a dataframe with its label
crosstable_peek_options

See which crosstable option is currently set.
crosstable_reset_options

Reset all crosstable options.
get_percent_pattern

Percent pattern helper
reexports

Objects exported from other packages
import_labels

Import labels
remove_labels

Remove all label attributes.
crosstable_test_args

Default arguments for calculating and displaying tests in crosstable()
test_tabular_auto

test for contingency table
test_survival_logrank

test for survival comparison
transpose_crosstable

Transpose a crosstable
write_and_open

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

test for correlation coefficients
summaryFunctions

Summary functions
set_label

Set the "label" attribute of an object
format_fixed

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

test for mean comparison
pivot_crosstable

Pivot a crosstable
test_summarize_linear_contrasts

Test for linear trend across ordered factor with contrasts
effect_tabular

Effect measure for association between two categorical variables
plim

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
N

Return the number of non NA observations
as_workbook

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

Add a legend to a table or a figure
as_flextable.crosstable

Turns a crosstable object into a formatted flextable
as_gt.crosstable

Converts a crosstable object into a formatted gt table.
body_add_gg2

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

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

Summarize a numeric vector
body_add_title

Add a title to an officer document
body_add_list

Add a list to an officer document
clean_names_with_labels

Cleans names of a dataframe while retaining old names as labels
body_add_table_list

Add a list of tables
body_add_table_section

Add a section with a table and its legend