is_almost_constant: Test whether a vector is almost constant

View source: R/is_almost_constant.R

is_almost_constantR Documentation

Test whether a vector is almost constant

Description

Check if a vector contains (almost) the same value in the majority of its elements. The function returns TRUE if the proportion of the most frequent value in x is greater than or equal to the specified threshold.

Usage

is_almost_constant(x, threshold = 1, na_rm = FALSE)

Arguments

x

A vector to be tested.

threshold

A numeric scalar in the interval [0,1] specifying the minimum required proportion of the most frequent value. Defaults to 1.

na_rm

Logical; if TRUE, NA values are removed before computing proportions. If FALSE, NA is treated as an ordinary value, so a large number of NAs can cause the function to return TRUE.

Details

This is useful for detecting low-variability or degenerate variables, which may be uninformative in modeling or analysis.

Value

A logical scalar. Returns TRUE in the following cases:

  • x is empty or has length one.

  • x contains only NA values.

  • The proportion of the most frequent value in x is greater than or equal to threshold. Otherwise, returns FALSE.

Author(s)

Michal Burda

See Also

remove_almost_constant(), unique(), table()

Examples

is_almost_constant(1)
is_almost_constant(1:10)
is_almost_constant(c(NA, NA, NA), na_rm = TRUE)
is_almost_constant(c(NA, NA, NA), na_rm = FALSE)
is_almost_constant(c(NA, NA, NA, 1, 2), threshold = 0.5, na_rm = FALSE)
is_almost_constant(c(NA, NA, NA, 1, 2), threshold = 0.5, na_rm = TRUE)


nuggets documentation built on Nov. 5, 2025, 6:25 p.m.