add_nobs.RdAdd 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)a gtsummary table of class "tbl_summary"
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
if TRUE (default), the presentation of the missing data will be removed from table
a gtsummary table with the Nobs
#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
tab |> add_nobs (location = "col")
Characteristic
Nobs
Overall
N = 2281
Nobs
Female
N = 1381
Nobs
Male
N = 901
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
1 n (%); Median (Q1, Q3)