Mode in R (4 Programming Examples)

 

In this tutorial, I’ll explain how to compute the mode in R.

The article contains four examples for different data scenarios.

So without further ado, let’s jump right to the examples.

 

Example 1: Create User-Defined Mode Function in R

Often confusing: The R programming language provides a built in mode function in Base R. However, this function is not computing the statistical mode, but something else.

For that reason, we need to create our own user-defined R function to calculate the mode. We can create such a manual mode function as follows:

my_mode <- function(x) {                     # Create mode function 
  unique_x <- unique(x)
  tabulate_x <- tabulate(match(x, unique_x))
  unique_x[tabulate_x == max(tabulate_x)]
}

You can simply copy, paste, and run this code in your own RStudio to make the user-defined mode function available.

Let’s create an example vector to see how our mode function works in practice:

x1 <- c(7, 1, 3, 8, 5, 7)                    # Create example vector

Now, let’s apply our mode function to this vector to see which category exists the most often:

my_mode(x1)                                  # Apply mode function
# 7

The mode of our example vector is 7.

 

Example 2: Apply Mode Function to Data with Multiple Modes

In reality it might happen that a data vector contains several values with the same frequency. Let’s create another example vector to see how our mode function behaves in such a case:

x2 <- c(7, 1, 3, 8, 5, 7, 3)                 # Create vector with several modes

As you can see, our new example vector has two modes (7 and 3).

Now, let’s apply our mode function to this vector:

my_mode(x2)                                  # Apply mode function
# 7 3

The output is 7 and 3 – looks good!

 

Example 3: Apply Mode Function to Real Data Frame Column

Next, I’ll apply our mode function to a data frame column of a real data set in order to give you a more realistic example.

For the example I’m going to use the Iris Flower data. So let’s load the data and let’s see how the data looks like:

data(iris)                                   # Load iris data
head(iris)                                   # First six rows of iris data

 

nrow function in R - Iris Example Data Frame

Table 1: The First 6 Rows of the Iris Flower Data Matrix.

 

Now, let’s use our mode function to extract the mode(s) of the first column of the Iris data:

my_mode(iris$Petal.Length)                   # Apply mode function to column
# 1.4 1.5

The column has two mode values: 1.4 and 1.5.

Note: We just applied our mode function to a numerical column. However, we could also use our mode function for categorical data (i.e. factors or character strings).

 

Example 4: Pre-Defined Mode Function (DescTools R Package)

You don’t want to create a mode function yourself (as we did in the beginning of this tutorial)? No worries, there are R add-on packages available that do also contain mode functions.

For instance, the DescTools package contains the Mode (notice the upper case M) function.

In order to use the function, we need to install and load the DescTools R package first:

install.packages("DescTools")                # Install DescTools package
library("DescTools")                         # Load DescTools package

Now, we can apply the Mode function to our first example vector…

Mode(x1)                                     # Apply DescTools Mode function
# 7

…or to our second example vector…

Mode(x2)                                     # Apply DescTools Mode function
# 3 7

…or to the Iris Flower data set:

Mode(iris$Petal.Length)                      # Apply DescTools Mode function
# 1.4 1.5

As you can see, the Mode function returns the same values as our own function to the RStudio console. If you prefer to create your own function or to load a new package is a matter of taste – the results are the same.

 

Further Resources & Summary

At this point you should know how to calculate the mode in the R programming language. However, being able to compute the mode doesn’t mean that you understood the statistical research concept of the mode.

If you want to learn more about that you could have a look at the following YouTube video. In the video, Sagar Jain explains the statistical concept of the mode in detail.

 

 

Also, you might have a look at some of the related R tutorials of this website:

To summarize: This R programming tutorial illustrated how to calculate the mode in R (or RStudio). However, if you have any questions, don’t hesitate let me know in the comments section below. In addition, don’t forget to subscribe to my email newsletter for more R tutorials – it’s free.

 



Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top