ord_collapse: Compress integer ordinal factors with too many levels

Description Usage Arguments Value See Also Examples

View source: R/data_prep.R

Description

Polychoric correlation routines often limit the number of levels an ordinal (ordered categorical) factor can have. This function is like forcats::fct_collapse() but preserves the ordering of levels. Modified factors are releveled to 1:n.

Usage

1
ord_collapse(.data, maxlev = 10, newlev = maxlev)

Arguments

.data

data frame with ordinal factors coded as integers

maxlev

apply level collapsing only if nlevels is greater than this

newlev

if collapsing levels, how many levels should the factor have. Default is maxlev.

Value

data frame with same columns

See Also

Other data munging: drop_nzv(), dummy_code()

Examples

1
2
3
4
5
library(dplyr)
mtcars %>%
  mutate(across(c("cyl", "vs", "am", "gear", "carb", "disp"), as.ordered)) %>%
  ord_collapse() %>%
  summarise_all(nlevels)

anchorlytics/anchorUtils documentation built on Oct. 16, 2020, 7:11 p.m.