Find Indices of Multiple Maxima & Minima in R (2 Examples)


In this tutorial, I’ll show how to identify the positions of multiple minima and maxima in R programming.

Table of contents:

Let’s start right away!


Constructing Example Data

At the start, let’s create some example data:

vec <- c(1, 2, 2, 3, 2, 3, 3, 1)    # Create example vector
vec                                 # Print example vector
# [1] 1 2 2 3 2 3 3 1

Have a look at the previous output of the RStudio console. It shows that our example data is a vector object containing eight integer elements.


Example 1: Get Index Position of First Maximum & Minimum Using which.max() & which.min() Functions

In this example, I’ll explain how to return the first maximum and minimum position of a vector object.

To find the very first occurrence of a maximum in our data, we can use the which.max function as shown below:

which.max(vec)                      # Find first maximum
# [1] 4

The previous output returns the value 4, i.e. the element at the fourth index position of our vector contains the first occurrence of the maximum value (i.e. 3).

Similar to that, we can apply the which.min function:

which.min(vec)                      # Find first minimum
# [1] 1

The minimum in our data (i.e. the value 1) appears at the very first index position in our vector.

As you have seen in this example, the which.max and which.min functions can be used to find the first position of a maximum or minimum value. However, in case a vector object (or the column of a data frame) contains the same maximum or minimum value at multiple positions, we have to apply a different syntax.

Next, I’ll explain how to identify all maxima and minima in a variable.


Example 2: Get Index Positions of All Maxima & Minima Using which(), max() & min() Functions

In Example 2, I’ll demonstrate how to get each maximum and minimum index position in a vector.

For this task, we can apply the which and max functions to return the indices of the maxima…

which(vec == max(vec))              # Find all maxima
# [1] 4 6 7

…and the which and min functions to return the positions of all minima:

which(vec == min(vec))              # Find all minima
# [1] 1 8


Video, Further Resources & Summary

If you need further info on the R syntax of this article, I recommend having a look at the following video on my YouTube channel. I explain the topics of this article in the video.



In addition, you might want to have a look at the related R programming tutorials on my website:


This page has demonstrated how to find the index positions of multiple minima and maxima in the R programming language. In case you have further questions, let me know in the comments. Furthermore, don’t forget to subscribe to my email newsletter for regular updates on the newest articles.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

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.