fct_lump_lowfreq2: Lump together infrequent levels

Description Usage Arguments Details Value Examples

Description

This function is a variant of fct_lump_lowfreq from forcats, but instead of lumping together levels such that the "Other" category is smaller than all others, it lumps together levels such that the "Other" category is smaller than the kth-most numerous, for some k.

Usage

1
fct_lump_lowfreq2(f, other_level = "Other", k = 1, prop = 1)

Arguments

f

A factor

other_level

The name to use for the "Other" level. Defaults to "Other".

k, prop

The function compares to the kth-largest element (rounds up for non-integer positive numbers), and ensures the "Other" level is no larger than prop times the kth-most numerous. Defaults to 1.

Details

As you might imagine, this function borrows heavily from the source code from forcats for fct_lump_lowfreq, and is used with the appropriate permission.

Value

A reordered factor

Examples

1
2
3
4
5
6
7
# Requires magrittr for pipes
x <- factor(rep(LETTERS[1:9], times = c(40, 10, 5, 27, 1, 1, 1, 1, 1)))
x %>% table()
x %>% fct_lump_lowfreq2() %>% table()
x %>% fct_lump_lowfreq2(k = 3, other = "Others") %>% table()
x %>% fct_lump_lowfreq2(prop = 1.5) %>% table()
# (Examples modified from forcats)

AlexJHomer/morecats documentation built on Dec. 17, 2021, 7:50 a.m.