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)A data frame with updated variable labels.
The function iterates over all columns in the dataset and performs the following steps:
Retrieves the current label of each variable using labelled::var_label.
Creates a new label by appending the text "n (dm ; %dm)" to the existing label.
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.
# 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.)"
#>