Tests if almost all values in a vector are the same.
Source:R/is_almost_constant.R
is_almost_constant.Rd
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.
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, ifna_rm
isTRUE
, the proportion is calculated from non-NA
values only. Ifna_rm
isFALSE
, the proportion is calculated from all values and the valueNA
is considered as a normal value (i.e., too muchNA
s 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
.
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