epi_clean_drop_zero_levels_vector: Drop Unused Levels from a Factor Vector

View source: R/epi_clean_drop_zero_levels.R

epi_clean_drop_zero_levels_vectorR Documentation

Drop Unused Levels from a Factor Vector

Description

Removes any factor levels that have zero occurrences, which can arise after introducing NA values or subsetting. Handy for cleaning up factor vectors before analysis or modeling.

Usage

epi_clean_drop_zero_levels_vector(factor_var)

Arguments

factor_var

A factor vector in which you want to drop unused levels.

Value

A factor vector with all levels that have zero observations removed.

Examples

## Not run: 
# Single vector example
factor_var <- factor(c("a", "b", "a", "c", "b", "a", "b", "c"))
factor_var[c(2, 5)] <- NA # simulate missing values that remove counts
cleaned_factor_var <- epi_clean_drop_zero_levels_vector(factor_var)
print(cleaned_factor_var)

# Data frame example: apply to each factor column
df <- data.frame(
  col1 = factor(c("a", "b", "a", "c", "b")),
  col2 = factor(c("d", "d", "e", "f", "d")),
  col3 = factor(c("g", "h", "g", "g", "h"))
)
df$col1[c(2, 5)] <- NA
df$col2[4] <- NA
df$col3[5] <- NA
df[] <- lapply(df, function(column) {
  if (is.factor(column)) {
    epi_clean_drop_zero_levels_vector(column)
  } else {
    column
  }
})
print(df)

## End(Not run)

AntonioJBT/episcout documentation built on June 11, 2025, 7:26 p.m.