# Find Index of Maximum & Minimum Value of Vector & Data Frame Row in R (2 Examples)

In this article you’ll learn how to **identify the index of maxima and minima of vectors and data frames** in the R programming language.

The tutorial will consist of this information:

- Example 1: Determine Index of Maximum & Minimum of Vector
- Example 2: Determine Index of Maximum & Minimum of Data Frame Column
- Video, Further Resources & Summary

Let’s dive into it…

## Example 1: Determine Index of Maximum & Minimum of Vector

Example 1 explains how to find the max and min value of a numeric vector in R. First, we need to create an example vector:

vec <- c(3, 1, 8, 3, 2, 5) # Create example vector

Our example vector consists of six numeric vector elements. Now, we can apply the which.max function to find the index of the maximum value…

which.max(vec) # Identify index of max value # 3

…and the which.min function to determine the index of the minimum:

which.min(vec) # Identify index of min value # 2

As you can see based on the previous outputs of the RStudio console, the max value is at position 3 and the min value is located at position 2 of our example vector.

## Example 2: Determine Index of Maximum & Minimum of Data Frame Column

We can use a similar R syntax as in Example 1 to determine the row index of the max or min value of a data frame column. First, we have to create some example data:

data <- data.frame(x1 = c(7, 8, 1, 4, 0, 5), # Create example data frame x2 = letters[1:6]) data # Print data to RStudio console # x1 x2 # 1 7 a # 2 8 b # 3 1 c # 4 4 d # 5 0 e # 6 5 f

Our data matrix contains two columns and six rows. Let’s assume that we want to find the index of the rows with the min and max values of our variable x1. Then, we can apply the which.max…

which.max(data$x1) # Apply which.max function to column # 2

…and which.min functions:

which.min(data$x1) # Apply which.min function to column # 5

The highest value of the column x1 is located in row 2 and the lowest value of the column x1 is located in row 5.

## Video, Further Resources & Summary

If you need more information on the R programming code of this post, you may have a look at the following video of my YouTube channel. I illustrate the R programming code of this page in the video:

Furthermore, I can recommend to read the related tutorials of this website. Please find a selection of articles here.

In summary: At this point you should know how to **get row-wise minima and maxima** in the R programming language. If you have any further questions, let me know in the comments.

## 8 Comments. Leave new

if the minimum value in at 2 places then what is the syntax

Hey Madhulika,

Are you looking for this?

https://statisticsglobe.com/find-indices-multiple-maxima-minima-r

Regards,

Joachim

Hi. I have two columns. One consisting of the names of story books and novels and another consisting of their ratings. How do I find the name of the novel with the lowest rating?

Hey Neena,

Please have a look at the following example:

Regards,

Joachim

Hi Joachim.

This works well for me. Thank you.

Regards

Neena

Hi Neena,

That’s great to hear, glad it helped! ðŸ™‚

Regards,

Joachim

Hi, If we have three columns

data <- data.frame(number = c(4,6,7),

ratings = c(3, 4, 6),

sales=c(1,1,7)

)

data

How do I find the name of the variable with the min, and how many values in that variable are smaller than the other two variables? which is here sales and 2 values smaller.

Hi Jack,

I’m sorry for the delayed reply. I was on a long vacation, so unfortunately I wasn’t able to get back to you earlier. Do you still need help with your syntax?

Regards,

Joachim