This function appends the text "n (dm ; %dm)" to the labels of all variables in a dataset. It uses the labelled package to modify and update variable labels in-place.

ajouter_label_ndm(data, col_to_skip = NULL)

Arguments

data

A data frame containing the dataset whose variable labels need to be updated.

col_to_skip

A column to skip when adding "n (dm ; %dm)". Default is NULL.

Value

A data frame with updated variable labels.

Details

The function iterates over all columns in the dataset and performs the following steps:

  1. Retrieves the current label of each variable using labelled::var_label.

  2. Creates a new label by appending the text "n (dm ; %dm)" to the existing label.

  3. Updates the variable's label using labelled::set_variable_labels.

This is useful when preparing a dataset for descriptive analysis, where it is helpful to display missing data statistics (n, dm, and %dm) alongside variable labels in summary tables.

Examples

# Example usage:
library(labelled)
#> Warning: package 'labelled' was built under R version 4.4.3

# Create a sample dataset
data <- data.frame(
  var1 = c(1, 2, NA),
  var2 = c("A", "B", NA)
)

# Assign initial labels
data <- labelled::set_variable_labels(
  data,
  var1 = "Variable 1",
  var2 = "Variable 2"
)

# Add "n (dm ; %dm)" to labels
data <- ajouter_label_ndm(data)

# Check updated labels
labelled::var_label(data)
#> $var1
#> [1] "Variable 1 n (d.m.)"
#> 
#> $var2
#> [1] "Variable 2 n (d.m.)"
#>