weighted_mode: Weighted mode

Description Usage Arguments Details Value Examples

View source: R/computations.R

Description

Calculate the weighted mode of a vector.

Usage

1
weighted_mode(x, w = rep(1, length(x)), na.rm = FALSE)

Arguments

x

An atomic vector.

w

A numeric vector of weights, the same length as x. The default gives each element of x equal weight.

na.rm

Should missing values in x and w be removed?

Details

Returns the value(s) in x with the largest weight, which by default is the most frequently occurring values in x. This is equivalent to choosing a value m that minimizes ∑ w[x != m].

The mode need not be unique, and a warning is given in this case.

Value

A vector of the same type as x that gives the mode(s).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Unique mode
weighted_mode(c(1, 3, 2, 1))
weighted_mode(c(TRUE, FALSE, TRUE, FALSE), 1:4)

# Two modes
weighted_mode(c("a", "b", "c", "b", "c"))

# Notice that the mode is known despite the NA
weighted_mode(c(1, 1, 1, 2, NA))

# But this isn't always the case
weighted_mode(c(1, 1, 2, NA))

marberts/smart documentation built on March 9, 2021, 5:31 p.m.