Home » How to Calculate the Mode in R (With Examples)

How to Calculate the Mode in R (With Examples)

by Tutor Aspire

The mode 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 == 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 == max(tab)]
}

#define numeric vector
data #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 == max(tab)]
}

#define numeric vector with multiple modes
data #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 == max(tab)]
}

#define character vector
data #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.

Additional Resources

The following tutorials explain how to calculate other descriptive statistics in R:

How to Calculate Five Number Summary in R
How to Create Summary Tables in R
How to Use the mean() Function in R

You may also like