Ties are dealt with by either: randomly selecting from the available values, thus always returning a vector of length 1; or returning all tied values, thus potentially returning a vector of variable length.

get_mode(x, ties = "sample", ...)

Arguments

x

Vector

ties

Character. If ties = "sample", one value from the available values will be randomly selected. Otherwise all ties are returned. Use anything other than "sample" to return all ties.

...

Passed to base::table(). Use for NA handling. Note default handling is useNA = "no", thus NA values will be ignored.

Value

Vector of the most frequent element(s) of x, with the same class as x. If ties = "sample", the length of the returned vector will be 1.

Examples

x <- sample(c(letters, rep(NA, 10)), 100, replace = TRUE)
get_mode(x)
#> [1] "a"
x <- x |> factor(levels = letters)
get_mode(x)
#> [1] r
#> Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z
get_mode(x, useNA = "always")
#> [1] <NA>
#> Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z

x <- c(10, 10, 20, 20)
get_mode(x)
#> [1] 20
get_mode(x, ties = "blah")
#> [1] 10 20