Learn R Programming

vcdExtra (version 0.9.6)

collapse_levels: Collapse the levels of a dataset

Description

Collapses the levels of a dataset (of any form) into those specified. May also be used to re-name levels. Ensure argument freq is supplied should your data be in frequency form (and the frequency column differs in name from default, "Freq").

Usage

collapse_levels(x, freq = "Freq", ...)

Value

The collapsed dataset in its original form (i.e., the initial form of x).

Arguments

x

The dataset to be collapsed.

freq

Supply only if your data is in frequency form AND your frequency column differs in name from the default ("Freq").

...

A collection of one or more assignments of dataset variables to a list of levels in the format new_level = c("old_level_1", "old_level_2", ..., "old_level_n").

Author

Gavin M. Klorfine

Details

First converts the object x into a frequency form data frame. Then, fct_collapse is used to collapse variable levels. Next, duplicate rows (an artefact of collapsing) are aggregated via summarise. Last, the frequency form data frame is converted back into the initial form of object x.

The exceptions to this are objects in case form, which are passed directly to fct_collapse (and duplicate rows are not aggregated).

See Also

fct_collapse, collapse.table

Tidy conversion functions: link{as_table}, link{as_freqform}, link{as_caseform}, link{as_matrix}, link{as_array},

Examples

Run this code
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