How to Calculate the Mode in R (With Examples)

The mode in R is the most frequently occurring value in a vector. To calculate the mode in R, you can use the table() or findMode() functions. The table() function creates a frequency table of the data set and then the max() function can be used to identify the most frequent value. The findMode() function is a convenient command that does all of this in one line of code. Examples are included to help show how these functions can be used to calculate the mode.


The of a dataset represents the most frequently occurring value.

In any given dataset, there can be no mode, one mode, or multiple modes.

The statistical software R does not have a built-in function to calculate the mode of a dataset, but you can use the following function to calculate the mode:

find_mode <- function(x) {
  u <- unique(x)
  tab <- tabulate(match(x, u))
  u[tab == max(tab)]
}

The following examples show how to use this function in practice.

Example 1: Calculating the Mode of A Numeric Vector

The following code shows how to use this function to calculate the mode of a numeric vector

#define function to calculate mode
find_mode <- function(x) {
  u <- unique(x)
  tab <- tabulate(match(x, u))
  u[tab == max(tab)]
}

#define numeric vector
data <- c(1, 2, 2, 3, 4, 4, 4, 4, 5, 6)

#find mode
find_mode(data)

[1] 4

The mode of the dataset turns out to be 4. This is the number that occurs most frequently.

Note that we can also use this function when there are multiple modes in a dataset:

#define function to calculate mode
find_mode <- function(x) {
  u <- unique(x)
  tab <- tabulate(match(x, u))
  u[tab == max(tab)]
}

#define numeric vector with multiple modes
data <- c(1, 2, 2, 2, 2, 3, 4, 4, 4, 4, 5, 6)

#find mode
find_mode(data)

[1] 2 4

The modes of the dataset are 2 and 4. Both of these numbers occur most frequently.

Example 2: Calculating the Mode of a Character Vector

This function can also be used to calculate the mode of a character vector:

#define function to calculate mode
find_mode <- function(x) {
  u <- unique(x)
  tab <- tabulate(match(x, u))
  u[tab == max(tab)]
}

#define character vector
data <- c('Sunny', 'Cloudy', 'Sunny', 'Sunny', 'Rainy', 'Cloudy')
#find mode
find_mode(data)

[1] "Sunny"

The mode turns out to be “Sunny” – this is the  string that occurs most often in the vector.

x