Logo

How to find the statistical mode in R?

The “mode” of a dataset is the most frequently occurring value(s). Unlike mean or median, R doesn’t offer a built-in function for the mode, so you often create a custom function or use a specialized package.

Custom Base R Function
Here’s a quick way to get all modes (in case of ties) using base R:

mode_func <- function(x) { freq_table <- table(x) max_freq <- max(freq_table) names(freq_table)[freq_table == max_freq] } # Example usage v <- c(2, 3, 3, 5, 2, 3, 7) mode_func(v) # Returns "3" because 3 appears most frequently
  1. table(x) counts how often each unique value appears.
  2. max(freq_table) finds the highest frequency.
  3. names(freq_table)[freq_table == max_freq] returns the value(s) that have that highest frequency.

Using a Package (Optional)
Some packages, like DescTools, include a built-in Mode() function:

install.packages("DescTools") library(DescTools) Mode(v)

This can be more convenient if you prefer not to define a custom function.

Note on Data Types

  • For numeric vectors, the above approach works fine.
  • For character or factor vectors, you can use the same logic; just be aware that table() treats them as categories.

Further Learning
If you’re enhancing your R and problem-solving skills for coding interviews, these courses can help:

If you’re also interested in designing scalable systems, check out Grokking System Design Fundamentals. For personalized guidance, consider a Coding Mock Interview with ex-FAANG engineers. You can also explore the DesignGurus.io YouTube channel for concise tutorials and insights.

CONTRIBUTOR
TechGrind