R/ordonner_var_qualitatives_function.R
ordonner_variables_qualitatives.RdThis function reorders the levels of all qualitative (factor) variables in a dataset based on their frequency, in descending order. It ensures that the most frequent levels appear first when analyzing or visualizing the data.
ordonner_variables_qualitatives(data)A data frame with reordered levels for all factor variables. Non-factor variables remain unchanged.
The function applies the following transformations:
Identifies all columns of type factor in the dataset.
Reorders the levels of each factor variable using the forcats::fct_infreq() function,
which orders levels by decreasing frequency.
This is particularly useful for preparing datasets for visualization or analysis, where it can be helpful to have the most common levels displayed first.
# Example usage:
library(dplyr)
library(forcats)
#> Warning: package 'forcats' was built under R version 4.4.3
# Create a sample dataset
data <- data.frame(
var1 = factor(c("A", "B", "A", "C", "B", "B")),
var2 = factor(c("X", "Y", "X", "Y", "X", "Z")),
var3 = c(1, 2, 3, 4, 5, 6) # Non-factor variable
)
# Reorder qualitative variables by frequency
data <- ordonner_variables_qualitatives(data)
# Check the new order of levels
levels(data$var1) # Output: "B" "A" "C"
#> [1] "B" "A" "C"
levels(data$var2) # Output: "X" "Y" "Z"
#> [1] "X" "Y" "Z"