Return the most common value in a sample or, for numerix x with no duplicate values, return the maximum density estimate.

sampleMode(x, na.rm = FALSE)

Arguments

x

A vector or matrix.

na.rm

If TRUE, remove NAs from x, if FALSE (default) retain them.

Value

A single value corresponding to the most common value in x.

Details

Returns the mode (most common value) of a vector or matrix or, for numerix x with no duplicate values, return the maximum density estimate. For non-numeric x with no duplicates return NA. When there is more than 1 mode, return the one whose value occurs first in x.

Author

David Braze davebraze@gmail.com

Examples

x0 <- sample(1:26, 5000, replace=TRUE) sampleMode(x0)
#> [1] 21
x1 <- sample(letters, 5000, replace=TRUE) sampleMode(x1)
#> [1] "c"
x2 <- rnorm(100000, m=9.5) sampleMode(x2)
#> Warning: WARNING: No value occurs more than once! Returning maximum density estimate for numeric x.
#> [1] 9.505601