Skip to contents

Function tests if almost all values in a vector are the same. The function returns TRUE if the proportion of the most frequent value is greater or equal to the threshold argument.

Usage

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

Arguments

x

a vector to be tested

threshold

a double scalar in the interval \([0,1]\) that specifies the threshold for the proportion of the most frequent value

na_rm

a flag indicating whether to remove NA values before testing the proportion of the most frequent value. That is, if na_rm is TRUE, the proportion is calculated from non-NA values only. If na_rm is FALSE, the proportion is calculated from all values and the value NA is considered as a normal value (i.e., too much NAs can make the vector almost constant too).

Value

If x is empty or has only one value, the function returns TRUE. If x contains only NA values, the function returns TRUE. If the proportion of the most frequent value is greater or equal to the threshold argument, the function returns TRUE. Otherwise, the function returns FALSE.

Author

Michal Burda

Examples

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