data("HairEyeColor") # Table form data
str(HairEyeColor)
collapse_levels(
HairEyeColor, # Dataset
Hair = list( # List of arguments for first variable
Dark = c("Black", "Brown"), # Collapse "Black" and "Brown" -> "Dark"
Light = c("Blond", "Red") # Collapse "Blond" and "Red" -> "Light"
),
Eye = list( # List of arguments for second variable
Common = c("Brown"), # Collapse (rename) "Brown" -> "Common"
Uncommon = c("Blue", "Green", "Hazel")
)
) |> str()
# To illustrate `freq` argument usage, convert Hoyt dataset to frequency form
# (ff) and then rename frequency column to "n"
data("Hoyt", package = "vcdExtra")
ff_Hoyt <- as_freqform(Hoyt)
names(ff_Hoyt)[length(ff_Hoyt)] <- "n"
str(ff_Hoyt)
collapse_levels(
ff_Hoyt,
# Ensure to supply if data is in frequency form and frequency column name
# differs from "Freq"
freq = "n",
Occupation = list(
High = c(1, 2),
Middle = 3,
Low = 4,
VeryLow = c(5, 6, 7)
)
) |> str()
Run the code above in your browser using DataLab