Skip to contents

This function calculates the mode of a given factor or vector that can be coerced into a factor. You can optionally provide prior weights for each level of the factor.

Usage

getMode(v, prior)

Arguments

v

A factor or vector that can be coerced into a factor. The mode will be calculated from the levels of this factor.

prior

A numeric vector of prior weights for each level of the factor. If not provided, all levels will be given equal weight.

Value

The mode of the factor v as a character string. If all values are NA, the function returns NA.

Examples

# Example 1: Mode without priors
v <- factor(c("apple", "banana", "apple", "orange", NA))
getMode(v)
#> [1] "apple"

# Example 2: Mode with priors
v <- factor(c("apple", "banana", "apple", "orange", NA))
prior <- c(apple = 0.5, banana = 1.5, orange = 1)
getMode(v, prior)
#> [1] "banana"