Open In App

How to extract the dataframe row with min or max values in R ?

Improve
Improve
Like Article
Like
Save
Share
Report

The tabular arrangement of rows and columns to form a data frame in R Programming Language supports many ways to access and modify the data. Application of queries and aggregate functions, like min, max and count can easily be made over the data frame cell values. Therefore, it is relatively very easy to access a subset of the data frame based on the values contained in the cell. 

Example 1: Determining the row with min or max value based on the entire data frame values.

An iteration is made over the data frame cells, by using two loops for each row and column of the data frame respectively. The cell value is compared to the initial minimum and maximum values respectively and updated in case the value satisfies the constraint. Also, a variable is declared to keep the current row index satisfying the condition. Then, the row at this index of the data frame is accessed. Time complexity is polynomial with respect to the size of the data frame. 

R




# declaring a data frame in R
data_frame = data.frame(C1 = c(5:8),
                        C2 = c(1:4),
                        C3 = c(9:12),
                        C4 = c(13:16))
 
print("Original data frame")
print(data_frame)
 
# declaring initial values for min and max
min = 32767
max = -32767
min_row_indx = 0
max_row_indx = 0
 
# looping over the data frame values
for (i in 1:nrow(data_frame)){
   
# for-loop over columns
for(j in 1:ncol(data_frame)) {  
   
        # checking if the dataframe
          # cell value is less than minimum
        if(data_frame[i,j]<min){
           
               # replacing the minimum
              # with the smaller value
            min = data_frame[i,j]
           
            # updating the row with the
              # smallest value found until now
            min_row_indx = i
        }
   
        # checking if the data frame
          # cell value is more than maximum
        if(data_frame[i,j]>max){
           
              # replacing the minimum
            # with the smaller value
            max = data_frame[i,j]
           
            # updating the row with the
              # smallest value found until now
            max_row_indx = i
        }
    }
}
# printing the row with minimum value
print ("Row with minimum value in the data frame")
print (data_frame[min_row_indx,])
 
# printing the row with maximum value
print ("Row with maximum value in the data frame")
print (data_frame[max_row_indx,])


Output:

[1] "Original data frame"
 C1 C2 C3 C4
1  5  1  9 13
2  6  2 10 14
3  7  3 11 15
4  8  4 12 16
[1] "Row with minimum value in the data frame"
 C1 C2 C3 C4
1  5  1  9 13
[1] "Row with maximum value in the data frame"
 C1 C2 C3 C4
4  8  4 12 16

Example 2: Determining the row with min or max value based on a data frame column

The function which.min() in R can be used to compute the minimum of all the values in the object specified as argument, whether it be a list, matrix, or data frame. Similarly, which.max() computes the largest of all the values. In order to select a particular column of the data frame we use df$colname and then apply this as an index of the data frame to extract the complete row with the specified aggregate function. This approach can be applied to all the data types, numeric, string as well as factor. The time complexity required is linear with respect to the column length since we directly access and compare all the values of this particular column. 

The following syntax in R is used to extract the row with minimum or maximum value in the column specified in the argument : 

Syntax: df[which.min(df$colname),]

Arguments : 

  • df – Data Frame to extract the minimum or maximum value from
  • colname – Column name to consider calculating minimum or maximum from.

Returns : The row with the maximum or minimum cell value in the particular column. 

Code:

R




# declaring a data frame in R
data_frame = data.frame(C1 = c(1:4),
                        C2 = c( 5:8),
                        C3 = c(9:12),
                        C4 = c(13:16))
 
print("Original data frame")
print(data_frame)
 
# extracting the row with
# maximum value in C2 column
print ("Row with max C2 value")
data_frame[which.max(data_frame$C2),]
 
# extracting the row with
# minimum value in C4 column
print ("Row with min C4 value")
data_frame[which.min(data_frame$C4),]


Output:

[1] "Original data frame"
 C1 C2 C3 C4
1  1  5  9 13
2  2  6 10 14
3  3  7 11 15
4  4  8 12 16
[1] "Row with max C2 value"
 C1 C2 C3 C4
4  4  8 12 16
[1] "Row with min C4 value"
 C1 C2 C3 C4
1  1  5  9 13

In case the data frame contains string type variable values, the minimum and maximum are computed upon sorting this data lexicographically. 

R




# declaring a data frame in R
data_frame = data.frame(C1= c("a","b","c","d"),
                        C2= c("geeks","dataframe","in","R"),
                        C3= c(9:12),C4=c(13:16))
 
print("Original data frame")
print(data_frame)
 
# extracting the row with maximum value in
# C2 column
print ("Row with max C1 value")
data_frame[which.max(data_frame$C1),]
 
# extracting the row with minimum value in
# C4 column
print ("Row with min C2 value")
data_frame[which.min(data_frame$C2),]


Output

[1] "Original data frame"
 C1        C2 C3 C4
1  a     geeks  9 13
2  b dataframe 10 14
3  c        in 11 15
4  d         R 12 16
[1] "Row with max C1 value"
 C1 C2 C3 C4
4  d  R 12 16
[1] "Row with min C2 value"
 C1        C2 C3 C4
2  b dataframe 10 14


Last Updated : 09 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads