Skip to contents

For each variable in a tbl_summary or tbl_likert table, add_n() adds a column with the total number of non-missing (or missing) observations.

Usage

add_n(
  x,
  statistic = "{N_nonmiss}",
  col_label = "**N**",
  footnote = FALSE,
  last = FALSE,
  ...
)

Arguments

x

(tbl_summary, tbl_likert)
A table created with tbl_summary() or tbl_likert().

statistic

(string)
The statistic to report. Default is the number of non-missing observations for each variable, statistic = "{N_nonmiss}". The available statistics are:

  • "{N_obs}" total number of observations,

  • "{N_nonmiss}" number of non-missing observations,

  • "{N_miss}" number of missing observations,

  • "{p_nonmiss}" percent non-missing data,

  • "{p_miss}" percent missing data.

Several statistics may be combined, e.g. statistic = "{N_nonmiss} / {N_obs} ({p_nonmiss}%)". The short forms {N}, {n}, {n_miss} and {p} are accepted for N_obs, N_nonmiss, N_miss and p_nonmiss.

col_label

(string)
The column header. Default is "**N**".

footnote

(scalar logical)
Whether to add a footnote naming the statistics shown. Default is FALSE.

last

(scalar logical)
Whether to place the N column last in the table. Default is FALSE, which places it after the label column.

...

Not used.

Value

A table of class c("tbl_summary", "ltsummary") with an n column.

Examples

# Example 1 ----------------------------------
trial |>
  tbl_summary(by = trt, include = c(age, grade, response)) |>
  add_n()
# Example 2 ---------------------------------- # show the number missing as well, with a footnote, as the last column trial |> tbl_summary(include = c(age, marker, response), missing = "no") |> add_n( statistic = "{N_nonmiss} / {N_obs} ({p_miss}% missing)", col_label = "**N (missing)**", footnote = TRUE, last = TRUE )