Drop Unused Factor Levels in all Factor Columns
Arguments
- data
data frame in which to remove unused levels in all columns that are factors
- dbg
if
TRUE
, debug messages are shown
Examples
# Create an example data frame with two factor columns
data <- data.frame(
id = 1:3,
factor_1 = factor(c("a", "b", "a"), levels = c("a", "b", "c")),
factor_2 = factor(c("x", "x", "y"), levels = c("x", "y", "z")),
no_factor = c("A", "B", "C"),
stringsAsFactors = FALSE
)
# Review the structure of the data frame
str(data)
#> 'data.frame': 3 obs. of 4 variables:
#> $ id : int 1 2 3
#> $ factor_1 : Factor w/ 3 levels "a","b","c": 1 2 1
#> $ factor_2 : Factor w/ 3 levels "x","y","z": 1 1 2
#> $ no_factor: chr "A" "B" "C"
# Review the structure of the data frame with unused factors removed
str(dropUnusedFactorLevels(data))
#> Removing unused factors from data$factor_1 ... ok. (0.00 secs)
#> Removing unused factors from data$factor_2 ... ok. (0.00 secs)
#> 'data.frame': 3 obs. of 4 variables:
#> $ id : int 1 2 3
#> $ factor_1 : Factor w/ 2 levels "a","b": 1 2 1
#> $ factor_2 : Factor w/ 2 levels "x","y": 1 1 2
#> $ no_factor: chr "A" "B" "C"