Open In App

Return the Index of the First Maximum Value of a Numeric Vector in R Programming – which.max() Function

Last Updated : 16 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

which.max() function in R Language is used to return the location of the first maximum value in the Numeric Vector.

Syntax: which.max(x)

Parameters:
x: Numeric Vector

Example 1:




# R program to find index of
# first maximum value
  
# Creating a vector
x <- c(2, 3, 4, 5, 1, 2, 3, 1, 2)
  
# Calling which.max() function
which.max(x)
  
# Printing maximum value
x[which.max(x)]


Output:

[1] 4
[1] 5

Example 2:




# R program to find index of
# first maximum value
  
# Calling pre-defined dataset
BOD$demand
  
# Calling which.max() function
which.max(BOD$demand)
  
# Printing maximum value
BOD$demand[which.max(BOD$demand)]


Output:

[1] 8.3 10.3 19.0 16.0 15.6 19.8
[1] 6
[1] 19.8

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads