modes: Identify multiple modes

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/modes.R

Description

Identifies (multiple) modes in a frequency vector.

Usage

1
modes(V, pos=FALSE, tolerance=0.1)

Arguments

V

A frequency vector

pos

Categories of frequency vector (optional)

tolerance

Specify how similar values have to be to be treated as different (optional). Differences smaller than or equal to the tolerance are ignored.

Details

This function identifies which positions of a frequency vector correspond to the mode. If there are multiple modes of the same value, all matching positions will be reported. Use the function collapse to create frequency vectors if necessary.

Value

The function returns a list. The at returns the categories of the frequency vector. Either these categories were specified using the argument pos, or we assume it to be 1:k (with k the number of categories in the frequency vector). If the length of the pos argument does not match the length of the frequency vector, a warning is shown, and the pos argument is ignored. The frequencies returns the frequency vector. The mode returns the value of the mode(s). If there are multiple modes, they are listed. Similar frequencies are counted as equal, using the tolerance argument. To prevent similar frequencies to be considered the same, set tolerance to 0. The positions returns the positions of the vector that correspond to the mode. This will differ from the mode if pos is provided.The contiguous returns TRUE of all modes are contiguous, and FALSE if there are different values in between. If there is only one mode, it is defined as contiguous (i.e. TRUE).

Author(s)

Didier Ruedin

See Also

secondModes

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Example 1: finding the mode
V1 <- c(30,40,210,130,530,50,10)
modes(V1) # will find position 5
# Example 2:
V2 <- c(3,0,4,1)
modes(V2) # will find position 3
# Example 3: providing categories
modes(V2,pos=-1:2) # will still find position 3, but give the value of 1 as mode
# Example 4: similar values
V3 <- c(30,40,500,130,530,50,10) 
modes(V3, tolerance=30) # will find positions 3 and 5 (500 and 530 are nearly the same)

Example output

$at
[1] 1 2 3 4 5 6 7

$frequencies
[1]  30  40 210 130 530  50  10

$mode
[1] 5

$positions
[1] 5

$contiguous
[1] TRUE

$at
[1] 1 2 3 4

$frequencies
[1] 3 0 4 1

$mode
[1] 3

$positions
[1] 3

$contiguous
[1] TRUE

$at
[1] -1  0  1  2

$frequencies
[1] 3 0 4 1

$mode
[1] 1

$positions
[1] 3

$contiguous
[1] TRUE

$at
[1] 1 2 3 4 5 6 7

$frequencies
[1]  30  40 500 130 530  50  10

$mode
[1] 3 5

$positions
[1] 3 5

$contiguous
[1] FALSE

agrmt documentation built on July 18, 2021, 1:06 a.m.