mfv: Most frequent value(s)

Description Usage Arguments Details Value Note References Examples

View source: R/mfv.R

Description

The function mfv() returns the most frequent value(s) (or mode(s)) found in a vector. The function mfv1 returns the first of these values, so that mfv1(x) is identical to mfv(x)[[1L]].

Usage

1
2
3
4
5
6
7
8
9
mfv(x, ...)

## Default S3 method:
mfv(x, na_rm = FALSE, ...)

## S3 method for class 'tableNA'
mfv(x, na_rm = FALSE, ...)

mfv1(x, na_rm = FALSE, ...)

Arguments

x

Vector of observations (of type numeric, integer, character, factor, or logical). x is to come from a discrete distribution.

...

Additional arguments (currently not used).

na_rm

logical. If TRUE, missing values do not interfer with the result, see 'Details'.

Details

See David Smith' blog post here to understand the philosophy followed in the code of mfv for missing values treatment.

Value

The function mfv returns a vector of the same type as x. One should be aware that this vector can be of length > 1, in case of multiple modes. mfv1 always returns a vector of length 1 (the first of the modes found).

Note

mfv() calls the function tabulate.

References

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Basic examples:
mfv(integer(0))                      # NaN
mfv(c(3, 3, 3, 2, 4))                # 3
mfv(c(TRUE, FALSE, TRUE))            # TRUE
mfv(c("a", "a", "b", "a", "d"))      # "a"

mfv(c("a", "a", "b", "b", "d"))      # c("a", "b")
mfv1(c("a", "a", "b", "b", "d"))     # "a"

# With missing values: 
mfv(c(3, 3, 3, 2, NA))               # 3
mfv(c(3, 3, 2, NA))                  # NA
mfv(c(3, 3, 2, NA), na_rm = TRUE)    # 3
mfv(c(3, 3, 2, 2, NA))               # NA
mfv(c(3, 3, 2, 2, NA), na_rm = TRUE) # c(2, 3)
mfv1(c(3, 3, 2, 2, NA), na_rm = TRUE)# 2

# With only missing values: 
mfv(c(NA, NA))                   # NA
mfv(c(NA, NA), na_rm = TRUE)     # NaN

# With factors
mfv(factor(c("a", "b", "a")))
mfv(factor(c("a", "b", "a", NA)))
mfv(factor(c("a", "b", "a", NA)), na_rm = TRUE)

statip documentation built on Nov. 18, 2019, 1:06 a.m.