leaf_other: Consolidate levels into other category

Description Usage Arguments Value Examples

View source: R/leaf_other.R

Description

Consolidate levels into other category

Usage

1
leaf_other(tbl, cutoff, other = "Other", inclusive = TRUE)

Arguments

tbl

A data frame with three columns – variable, level, and proportion, as created by leaf_peep()

cutoff

The proportion below which levels will be turned to other

other

A string indicating the name to be used for the other category, defaults to "Other"

inclusive

If TRUE, when a variable's other category is still below the cutoff, the next smallest level will also be converted to other. If FALSE, the other category may remain below the cutoff

Value

A data frame where levels with proportions below the cutoff are consolidated into an "other" category

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Set up an example data frame with some very small levels
df <- dplyr::tibble(
  variable   = c(rep("letter", 4), rep("number", 4)),
  level      = c(letters[1:4], 1:4),
  proportion = c(0.6, 0.395, 0.0045, 0.0045, 0.6, 0.395, 0.0055, 0.0035)
)

# Consolidate small categories so that all values are above the cutoff
leaf_other(df, cutoff = 0.005)

# You can give a different name to the other category
leaf_other(df, cutoff = 0.005, other = "REDACTED")
leaf_other(df, cutoff = 0.005, other = NA)

# If the other category is smaller than the cutoff, leaf_other() will convert
# the next smallest value to other, even if it is larger than the cutoff.
# Use `inclusive = FALSE` to only convert categories smaller than the cutoff.
leaf_other(df, cutoff = 0.005, inclusive = FALSE)

rossellhayes/leafpeepr documentation built on Feb. 29, 2020, 12:48 a.m.