weighted_mode: Weighted mode

Description Usage Arguments Details Value Author(s) Examples

View source: R/utilities.R

Description

Calculate the weighted mode of a vector.

Usage

1
weighted_mode(x, w, unique = TRUE, na.rm = FALSE)

Arguments

x

An atomic vector.

w

An optional vector of weights, the same length as x. Calculates an unweighted mode if no weights are given.

unique

Should multiple modes result in NA?

na.rm

Should missing values in x be removed?

Details

Returns the values in x with the largest weight. Leaving w blank, or giving a vector of equal weights, returns the most frequently occurring values in x. This is equivalent to choosing a value m that minimizes the weighted mean of the hamming distance between the elements of x and m.

The mode need not be unique, and NA is returned if unique is TRUE and there are multiple modes.

Value

A vector of the same type as x.

Author(s)

Steve Martin

Examples

1
2
3
4
5
6
7
8
## Not run: 
# 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'))

## End(Not run)

marberts/ppd documentation built on March 27, 2020, 7:21 p.m.