calc_mode: Mode - most frequent value of a variable

View source: R/calc_mode.R

calc_modeR Documentation

Mode – most frequent value of a variable

Description

Caclulate and return the most frequently occuring value of a vector.

Usage

calc_mode(x, na.rm = FALSE)

Arguments

x

A vector of values

na.rm

a logical value indicating whether NA values should be stripped before the computation proceeds.

Value

Returns the most frequent value. If there are more than one, all of them are returned in a vector.

References

https://www.tutorialspoint.com/r/r_mean_median_mode.htm https://rdrr.io/cran/DescTools/man/Mode.html

Examples

# Create the vector with numbers.
x <- c(2, 1, 2, 3, 1, 2, 3, 4, 1, 5, 5, NA, 2, 3)

# Calculate the mode using the user function.
calc_mode(x, na.rm = TRUE)
calc_mode(x, na.rm = FALSE)

library(DescTools)
data(d.pizza)

calc_mode(d.pizza$driver)
sapply(d.pizza[,c("driver","temperature","date")], calc_mode, na.rm = TRUE)

# Two values are returned if more than one mode
y <- c(2, 2, 2, 3, 3, 3)
calc_mode(y)

emilelatour/lamisc documentation built on April 9, 2024, 10:33 a.m.