How to extract the dataframe row with min or max values in R ?
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.
 "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  "Row with minimum value in the data frame" C1 C2 C3 C4 1 5 1 9 13  "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 :
- 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.
 "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  "Row with max C2 value" C1 C2 C3 C4 4 4 8 12 16  "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.
 "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  "Row with max C1 value" C1 C2 C3 C4 4 d R 12 16  "Row with min C2 value" C1 C2 C3 C4 2 b dataframe 10 14