add_p.tbl_summary: Adds p-values to summary tables

View source: R/add_p.R

add_p.tbl_summaryR Documentation

Adds p-values to summary tables


Adds p-values to tables created by tbl_summary by comparing values across groups.


## S3 method for class 'tbl_summary'
  test = NULL,
  pvalue_fun = NULL,
  group = NULL,
  include = everything(),
  test.args = NULL,



Object with class tbl_summary from the tbl_summary function


List of formulas specifying statistical tests to perform for each variable, e.g. list(all_continuous() ~ "t.test", all_categorical() ~ "fisher.test"). Common tests include "t.test", "aov", "wilcox.test", "kruskal.test", "chisq.test", "fisher.test", and "lme4" (for clustered data). See tests for details, more tests, and instruction for implementing a custom test.

Tests default to "kruskal.test" for continuous variables ("wilcox.test" when "by" variable has two levels), "" for categorical variables with all expected cell counts >=5, and "fisher.test" for categorical variables with any expected cell count <5.


Function to round and format p-values. Default is style_pvalue. The function must have a numeric vector input (the numeric, exact p-value), and return a string that is the rounded/formatted p-value (e.g. pvalue_fun = function(x) style_pvalue(x, digits = 2) or equivalently, purrr::partial(style_pvalue, digits = 2)).


Column name (unquoted or quoted) of an ID or grouping variable. The column can be used to calculate p-values with correlated data. Default is NULL. See tests for methods that utilize the ⁠group=⁠ argument.


Variables to include in output. Input may be a vector of quoted variable names, unquoted variable names, or tidyselect select helper functions. Default is everything().


List of formulas containing additional arguments to pass to tests that accept arguments. For example, add an argument for all t-tests, use test.args = all_tests("t.test") ~ list(var.equal = TRUE)


Not used


A tbl_summary object

Example Output

Example 1

image of rendered example table

Example 2

image of rendered example table


Daniel D. Sjoberg, Emily C. Zabor

See Also

See tbl_summary vignette for detailed examples

Review list, formula, and selector syntax used throughout gtsummary

Other tbl_summary tools: add_n.tbl_summary(), add_overall(), add_q(), add_stat_label(), bold_italicize_labels_levels, inline_text.tbl_summary(), inline_text.tbl_survfit(), modify, separate_p_footnotes(), tbl_custom_summary(), tbl_merge(), tbl_split(), tbl_stack(), tbl_strata(), tbl_summary()


# Example 1 ----------------------------------
add_p_ex1 <-
  trial[c("age", "grade", "trt")] %>%
  tbl_summary(by = trt) %>%

# Example 2 ----------------------------------
add_p_ex2 <-
  trial %>%
  select(trt, age, marker) %>%
  tbl_summary(by = trt, missing = "no") %>%
    # perform t-test for all variables
    test = everything() ~ "t.test",
    # assume equal variance in the t-test
    test.args = all_tests("t.test") ~ list(var.equal = TRUE)

gtsummary documentation built on July 26, 2023, 5:27 p.m.