How to extract the dataframe row with min or max values in R ?
Last Updated :
09 Dec, 2022
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
Similar Reads
How to Extract random sample of rows in R DataFrame with nested condition
In this article, we will learn how to extract random samples of rows in a DataFrame in R programming language with a nested condition. Method 1: Using sample() We will be using the sample() function to carry out this task. sample() function in R Language creates random samples based on the parameter
4 min read
Select First Row of Each Group in DataFrame in R
In this article, we will discuss how to select the first row of each group in Dataframe using R programming language. The duplicated() method is used to determine which of the elements of a dataframe are duplicates of other elements. The method returns a logical vector which tells which of the rows
2 min read
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 datafram
3 min read
Extract first N rows from dataframe in R
A required number of rows can be retrieved from a dataframe for some computation that demands so. In this article, we will be discussing two different approaches for extracting the first n rows from the data frame File in Use: Method 1: Using head() function This function will help to extract the gi
1 min read
Select rows from a DataFrame based on values in a vector in R
In this article, we will discuss how to select rows from a DataFrame based on values in a vector in R Programming Language. Method 1: Using %in% operator %in% operator in R, is used to identify if an element belongs to a vector or Dataframe. It is used to perform a selection of the elements satisfyi
5 min read
Extract vector from dataframe in R
In this article, we will see how to extract vectors from DataFrame in R Programming Language. The approach is simple, by using $ operator, we can convert dataframe column to a vector. Syntax: dataframe_name$columnname Given below are various examples to implement the same Example 1: [GFGTABS] R # cr
1 min read
How to Delete Row(s) in R DataFrame ?
In this article, we will see how row(s) can be deleted from a Dataframe in R Programming Language. Deleting a single row For this, the index of the row to be deleted is passed with a minus sign. Syntax: df[-(index), ] Example 1 :Â [GFGTABS] R # creating a data frame with # some data . df=data.frame(
2 min read
Select DataFrame Rows where Column Values are in Range in R
In this article, we will discuss how to select dataframe rows where column values are in a range in R programming language. Data frame indexing can be used to extract rows or columns from the dataframe. The condition can be applied to the specific columns of the dataframe and combined using the logi
2 min read
Return Column Name of Largest Value for Each Row in R DataFrame
In this article, we will discuss how to return column names of the largest value for each row in DataFrame in R Programming Language. Example: Â Column1Column2Column3Max columnRow1200Column1 , Because, Â Column2 value and Column 3 value is less than Column1Row2435Column3 , Because, Â Column2 value and
2 min read
How to select row with maximum value in each group in R Language?
In R Programming Language, to select the row with the maximum value in each group from a data frame, we can use various approaches as discussed below. Consider the following dataset with multiple observations in sub-column. This dataset contains three columns as sr_no, sub, and marks. Creating Datas
4 min read