mod: Computes a mod statistic.

Description Usage Arguments Value Examples

Description

A function for finding the most frequent element in a vector and optionally its frequency. The intended use is not for estimating mod statistic, but for finding a mod in sequences where elements repeat themselves often. Hence, it should not be used with vectors of real numbers, as such elements are likely to be unique. Note that the function is not capable of breaking ties randomly.

Usage

1
mod(x, freq = FALSE, ties = "unbroken", na.rm = TRUE)

Arguments

x

A numeric, logical or a character vector where the mod needs to be found.

freq

A logical that indicates whether frequency of elements in the mod should be returned as well. By default set to FALSE.

ties

A string that indicates how the ties should be broken. Possible values are: "unbroken" in which case ties are not broken and all of the tied elements are returned in the output (in this case, if freq is set to TRUE, frequency output will be a scalar that indicates frequency of all tied elements), and "first" where only the first element is returned. By default set to "unbroken".

na.rm

A logical that indicates whether NA elements should be removed or not. By default set to TRUE.

Value

If freq set to FALSE, the output is a vector of the same class as x with a single element, indicating the mod of the vector provided in x. If freq set to FALSE, the output is a list with first element being the mod and second element being the frequency of the mod.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# create some numeric vectors
x <- c(1,1,1,2,2)
y <- c(1,1,2,2)

# no ties, with and without reporting frequency
mod(x)
mod(x, freq = TRUE)

# ties, with and without reporting frequency
mod(y)
mod(y, freq = TRUE)

hstojic/hfunk documentation built on May 17, 2019, 6:16 p.m.