Learn R Programming

DataExplorer (version 0.4.0)

CollapseCategory: Collapse categories for discrete features

Description

Sometimes discrete features have sparse categories. This function will collapse the sparse categories for a discrete feature based on a given threshold.

Usage

CollapseCategory(data, feature, threshold, measure, update = FALSE,
  category_name = "OTHER")

Arguments

data

input data, in either data.frame or data.table format.

feature

name of the discrete feature to be collapsed.

threshold

the bottom x% categories to be collapsed, e.g., if set to 20%, categories with cumulative frequency of the bottom 20% will be collapsed.

measure

name of variable to be treated as additional measure to frequency.

update

logical, indicating if the data should be modified. Setting to TRUE will modify the input data directly, and will only work with data.table. The default is FALSE.

category_name

name of the bucket to group selected categories if update is set to TRUE. The default is "OTHER".

Value

If update is set to FALSE, returns categories with cumulative frequency less than the input threshold. The output class will match the class of input data.

Details

If a continuous feature is passed to the argument feature, it will be force set to character-class.

Examples

Run this code
# NOT RUN {
# load packages
library(data.table)

# generate data
data <- data.table("a" = as.factor(round(rnorm(500, 10, 5))), "b" = rexp(500, 1:500))

# view cumulative frequency without collpasing categories
CollapseCategory(data, "a", 0.2)

# view cumulative frequency based on another measure
CollapseCategory(data, "a", 0.2, measure = "b")

# collapse bottom 20% categories based on cumulative frequency
CollapseCategory(data, "a", 0.2, update = TRUE)
BarDiscrete(data)
# }

Run the code above in your browser using DataLab