This function creates descriptive tables for variables in a dataset. It can handle qualitative and quantitative variables, grouped or ungrouped, and supports multiple configurations for handling missing data (DM).

desc_var(
  data1,
  table_title = "",
  quali = NULL,
  quanti = NULL,
  add_total = TRUE,
  var_title = "Variable",
  by_group = FALSE,
  var_group = NULL,
  group_title = NULL,
  digits = list(mean_sd = 1, median_q1_q3_min_max = 1, pct = 1),
  drop_levels = TRUE,
  freq_relevel = FALSE,
  tests = FALSE,
  show_n_per_group = FALSE,
  show_missing_data = NULL,
  var_tot = NULL,
  var_characteristic = NULL
)

Arguments

data1

A data frame containing the dataset to be analyzed.

table_title

A character string specifying the title of the table.

quali

A vector of qualitative variables to be described. Defaults to NULL.

quanti

A vector of quantitative variables to be described. Defaults to NULL.

add_total

A boolean (default is TRUE) to add total column or not when var_group is specified.

var_title

A character string for the title of the variable column in the table. Defaults to "Variable".

by_group

A boolean (default is FALSE) to analyse by group.

var_group

A variable used for grouping (if applicable). Defaults to NULL.

group_title

A character string specifying the title for the grouping variable. Default is NULL and get the label or the variable.

digits

A list, the number of decimal places to round categorical and continuous variable. Default is list(mean_sd = 1, median_q1_q3_min_max = 1, pct = 1).

drop_levels

Boolean (default = TRUE). Drop unused levels.

freq_relevel

Boolean (default = FALSE). Reorder factors by frequency.

tests

A value in order to add p value. Default to FALSE OPTION :

  • FALSE: No p-value add

  • TRUE: Add p-value made by default by gtsummary. See gtsummary add_p() options.

  • list(): To force tests. See gtsummary add_p() options.

show_n_per_group

Default to FALSE. Should the 'N' appears in the column header of the groups. Can be either :

  • FALSE: No 'N' is shown

  • TRUE: 'N' is shown

show_missing_data

Default to NULL. Should the missing data be displayed. Can be either :

  • FALSE: No missing data displayed

  • TRUE: Missing data displayed

  • NULL (default): will be switch to anyNA(data1) value.

var_tot

A string specifying the name of total column. Default is NULL and will guess from theme_gtsummary_language().

var_characteristic

A string specifying the name of characteristic column. Default is NULL and will guess from theme_gtsummary_language().

Value

A gtsummary table object containing the descriptive statistics.

Details

The function processes the dataset according to the specified parameters and generates descriptive tables.

  • It first uses the ajouter_label_ndm() function to append missing data statistics to variable labels.

  • Depending on the group and DM arguments, it adjusts the dataset and creates tables using helper functions like desc_group, desc_degroup, and desc_degroup_group.

  • Qualitative variables are reordered, and unused levels are dropped when necessary.

Examples

# Example usage:
library(dplyr)

# Sample dataset
data1 <- data.frame(
  group = c("A", "B", "A", "C"),
  var1 = c(1, 2, 3, NA),
  var2 = c("X", "Y", "X", NA)
)

# Generate descriptive table
table <- desc_var(
  data1 = data1,
  table_title = "Descriptive Table"
)