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.

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

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

Value

a gtsummary table with the Nobs

Examples

#Data
library(dplyr)
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union
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)
#> Joining with `by = join_by(variable, var_type, var_label, row_type, label, n,
#> add_n_stat_1, add_n_stat_2, temp_table, temp_row, temp_na_stat)`
Characteristic Overall
N = 228
Female
N = 138
Male
N = 90
Yes/no variable, Nobs 228 138 90
    n (%) 138 (61%) 138 (100%) 0 (0%)
Institution code, Nobs 227 137 90
    Median (Q1, Q3) 11 (3, 16) 11 (3, 15) 11 (3, 16)
    Min - Max 1 - 33 1 - 33 1 - 33
Time, Nobs 228 138 90
    Median (Q1, Q3) 256 (167, 399) 224 (144, 371) 293 (194, 450)
    Min - Max 5 - 1,022 11 - 1,022 5 - 965
Age, Nobs 228 138 90
    Median (Q1, Q3) 63 (56, 69) 64 (57, 70) 61 (55, 68)
    Min - Max 39 - 82 39 - 82 41 - 77
ECOG score, Nobs 227 137 90
    0 63 (28%) 36 (26%) 27 (30%)
    1 113 (50%) 71 (52%) 42 (47%)
    2 50 (22%) 29 (21%) 21 (23%)
    3 1 (0.4%) 1 (0.7%) 0 (0%)
tab |> add_nobs (location = "col")
Characteristic Nobs Overall
N = 228
1
Nobs Female
N = 138
1
Nobs Male
N = 90
1
Yes/no variable 228 138 (61%) 138 138 (100%) 90 0 (0%)
Institution code 227
137
90
    Median (Q1, Q3)
11 (3, 16)
11 (3, 15)
11 (3, 16)
    Min - Max
1 - 33
1 - 33
1 - 33
Time 228
138
90
    Median (Q1, Q3)
256 (167, 399)
224 (144, 371)
293 (194, 450)
    Min - Max
5 - 1,022
11 - 1,022
5 - 965
Age 228
138
90
    Median (Q1, Q3)
63 (56, 69)
64 (57, 70)
61 (55, 68)
    Min - Max
39 - 82
39 - 82
41 - 77
ECOG score 227
137
90
    0
63 (28%)
36 (26%)
27 (30%)
    1
113 (50%)
71 (52%)
42 (47%)
    2
50 (22%)
29 (21%)
21 (23%)
    3
1 (0.4%)
1 (0.7%)
0 (0%)
1 n (%)
# 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")
Characteristic N = 2281
Gender, Nobs = 228
    Female 138 (61%)
    Male 90 (39%)
Yes/no variable, Nobs = 228 138 (61%)
Institution code, Nobs = 227 11 (3, 16)
Time, Nobs = 228 256 (167, 399)
Age, Nobs = 228 63 (56, 69)
ECOG score, Nobs = 227
    0 63 (28%)
    1 113 (50%)
    2 50 (22%)
    3 1 (0.4%)
1 n (%); Median (Q1, Q3)