Most frequently occurring value
stat_mode(x, na.rm = TRUE, ties = c("all", "first", "last", "missing"), ...)vector of the same class as x
# Character vector
chr_vec <- c("a", "d", "d", "h", "h", NA, NA) # Multiple modes
stat_mode(x = chr_vec)
#> [1] "d" "h"
stat_mode(x = chr_vec, na.rm = FALSE)
#> [1] "d" "h" NA
stat_mode(x = chr_vec, na.rm = FALSE, ties = "first")
#> [1] "d"
stat_mode(x = chr_vec, na.rm = FALSE, ties = "last")
#> [1] NA
# - # Numeric vector
# See that it keeps the original vector type
num_vec <- c(2, 3, 3, 4, 4, NA, NA)
stat_mode(x = num_vec)
#> [1] 3 4
stat_mode(x = num_vec, na.rm = FALSE)
#> [1] 3 4 NA
stat_mode(x = num_vec, na.rm = FALSE, ties = "first")
#> [1] 3
stat_mode(x = num_vec, na.rm = FALSE, ties = "last")
#> [1] NA
# The default option is ties="all" but it is very easy for the user to control
# the ties without changing this parameter.
# Select always just one mode, being that the first mode
stat_mode(x = num_vec)[1]
#> [1] 3
# Select the first and the second stat_mode
stat_mode(x = num_vec)[c(1, 2)]
#> [1] 3 4
# Logical Vectors
stat_mode(x = c(TRUE, TRUE))
#> [1] TRUE
stat_mode(x = c(FALSE, FALSE, TRUE, TRUE))
#> [1] FALSE TRUE
# - # Single element cases
stat_mode(x = c(NA_real_))
#> [1] NA
stat_mode(x = 2)
#> [1] 2
stat_mode(x = NA)
#> [1] NA
stat_mode(x = c("a"))
#> [1] "a"
# Not allowing multiple stat_mode, returning NA if that happens
stat_mode(x = c(1, 1, 2, 2), multiple_modes = FALSE) # multiple stat_mode
#> [1] 1 2
stat_mode(x = c(1, 1), multiple_modes = FALSE) # single mode
#> [1] 1
# Empty vector cases
# The ties of any empty vector will be itself (an empty vector of the same type)
stat_mode(x = double())
#> [1] NA
stat_mode(x = complex())
#> [1] NA
stat_mode(x = vector("numeric"))
#> [1] NA
stat_mode(x = vector("character"))
#> [1] NA