Package 'gtsummarytools'

Title: Add the Nobs to gtsummary tables
Description: Add the Nobs to gtsummary tables.
Authors: Marie Roumet
Maintainer: Marie Roumet <[email protected]>
License: GPL (>= 2)
Version: 0.1.2
Built: 2026-09-23 17:23:23 UTC
Source: https://github.com/dcr-unibe-ch/gtsummarytools

Help Index


add_nobs

Description

Add Nobs to a gtsummary table of class "tbl_summary" This function add the Nobs (i.e. number of non-missing data) to a gtsummary table. Depending on the specified location Nobs will be: (i) added in a new column, (ii) displayed as statistics alongside the variable name (iii) added after the variable name within the same cell.

Usage

add_nobs(table, location = "col", rm_missing = TRUE, nobs_text = "Nobs")

Arguments

table

a gtsummary table of class "tbl_summary"

location

the location for the Nobs.Can take the following values:

"col" = Nobs is displayed new column(s) (default)

"row" : Nobs is displayed as statistics alongside the variable name. Stats are presented below

"var_name": Nobs is added after the variable name within the same cell.This option is only available if the table is not stratified

rm_missing

if TRUE (default), the presentation of the missing data will be removed from table.

nobs_text

string indicating text shown for the number of missing observation. Default is "Nobs".

Value

a gtsummary table with the Nobs

Examples

#Data
library(dplyr)
library(labelled)
library(gtsummary)

data("cancer", package = "survival")#' 
cancer <- 
 cancer |> 
mutate(
  sex.factor = factor(sex, levels = c(1,2), labels = c("Female", "Male") ),
 yn_var = factor(sex, levels = c(1,2), labels = c("Yes", "No") )
) |>
set_variable_labels (
 yn_var = "Yes/no variable",
 sex.factor = "Gender",
 inst = "Institution code",
  time = "Time", 
 age = "Age",
 ph.ecog = "ECOG score") 
 

 # Ex1: stratified table
 #. gtsummaty table
tab <- 
  cancer |> 
  select(sex.factor, yn_var, inst, time,age, ph.ecog) |> 
  tbl_summary(
 by=sex.factor,
 type = all_continuous() ~ "continuous2",
 statistic = all_continuous() ~ c("{median} ({p25}, {p75})", "{min} - {max}"),
)  |> 
add_overall() 

 # .Add Nobs gtsummaty table 
tab |>  add_nobs (location = "row", rm_missing = TRUE, nobs_text ="No")
tab |> add_nobs (location = "col")
  
# Ex2: un-stratified table 
#. gtsummary table
tab <- 
  cancer |> 
  select(sex.factor, yn_var, inst, time,age, ph.ecog) |> 
  tbl_summary() 
  
 # .Add Nobs gtsummaty table 
 tab |>  
 add_nobs(location = "var_name")