Find the index of the maximum value in R DataFrame - GeeksforGeeks

11 Oct, 2021
In this article, we will see how to find the index of the maximum value from a DataFrame in the R Programming Language
We can find the maximum value index in a dataframe using the which.max() function.
Syntax:
which.max(dataframe_name$columnname)
“$” is used to access particular column of a dataframe.
Given below are various implementations, depicting various datatypes and situations to help uou understand better.
Example 1: 
R
# vector 1data1=c("sravan","bobby","pinkey","rohith","gnanesh") # vector 2data2=c(98,78,79,97,89) # creating a dataframe with names and marks# using above vectorsfinal <- data.frame(names=data1,marks=data2)                    print(final) # display the maximum value index in 2 nd column# (marks column) in a dataframeprint(paste("highest index is : ",which.max(final$marks)))
Output:
If there is more than one maximum value, then it will return index of first number which is repeated.
Example 2:
R
# vector 1data1=c("sravan","bobby","pinkey","rohith",        "gnanesh",'divya',"satwik","chandu") # vector 2data2=c(98,78,79,97,89,89,99,99) # creating a dataframe with names and marks# using above vectorsfinal <- data.frame(names=data1,marks=data2)                    print(final) print(paste("highest index is : ",which.max(final$marks)))
Output:
If the data is of type character, it will find the maximum value using ASCII values.
Example 3:
R
# vector 1data1=c("sravan","bobby","pinkey","rohith",        "gnanesh",'divya',"satwik","zhandu") # vector 2data2=c(98,78,79,97,89,89,99,99) # creating a dataframe with names and marks# using above vectorsfinal <- data.frame(names=data1,marks=data2)                    print(final) # display maximum value index for character valuesprint(paste("highest index is : ",which.max(final$names)))
Output:
Example 4:
R
# vector 1 that contains NA values as charactersdata1=c(NA,"sravan",NA,NA,NA) # vector 2 contains all datadata2=c(102,98,98,102,102) # creating a dataframe with names and marks# using above vectorsfinal <- data.frame(names=data1,marks=data2)                    print(final) # display maximum value index for character valuesprint(paste("highest index is : ",which.max(final$names))) # display maximum value index for  marks valuesprint(paste("highest index is : ",which.max(final$marks)))
Output:
If row containing all the values are same so all are higher. So it will return index of first element.
Example 5:
R
# vector contains all same datadata2=c(102,102,102,102,102) # creating a dataframe marks using above vectorfinal <- data.frame(marks=data2)                    print(final) # display maximum value index for  marks valuesprint(paste("highest index is : ",which.max(final$marks)))
Output:
If the data contains values as NA, then it will return empty.
Example 6:
R
# vector contains all NA'sdata2=c(NA,NA) # creating a dataframe marks using# above vectorfinal <- data.frame(marks=data2)                    print(final) # display maximum value index for  marks valuesprint(paste("highest index is : ",which.max(final$marks)))
Output:
prachisoda1234
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?
How to filter R dataframe by multiple conditions?
Convert Matrix to Dataframe in R