Find the index of the maximum value in R DataFrame
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 you understand better.
Example 1:
R
data1= c ( "sravan" , "bobby" , "pinkey" , "rohith" , "gnanesh" )
data2= c (98,78,79,97,89)
final <- data.frame (names=data1,marks=data2)
print (final)
print ( 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
data1= c ( "sravan" , "bobby" , "pinkey" , "rohith" ,
"gnanesh" , 'divya' , "satwik" , "chandu" )
data2= c (98,78,79,97,89,89,99,99)
final <- 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
data1= c ( "sravan" , "bobby" , "pinkey" , "rohith" ,
"gnanesh" , 'divya' , "satwik" , "zhandu" )
data2= c (98,78,79,97,89,89,99,99)
final <- data.frame (names=data1,marks=data2)
print (final)
print ( paste ( "highest index is : " , which.max (final$names)))
|
Output:
Example 4:
R
data1= c ( NA , "sravan" , NA , NA , NA )
data2= c (102,98,98,102,102)
final <- data.frame (names=data1,marks=data2)
print (final)
print ( paste ( "highest index is : " , which.max (final$names)))
print ( 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
data2= c (102,102,102,102,102)
final <- data.frame (marks=data2)
print (final)
print ( paste ( "highest index is : " , which.max (final$marks)))
|
Output:
If the data contains values as NA, then it will return empty.
Example 6:
R
data2= c ( NA , NA )
final <- data.frame (marks=data2)
print (final)
print ( paste ( "highest index is : " , which.max (final$marks)))
|
Output:
Last Updated :
11 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...