Min() and Max() Function in R

This chapter is dedicated to min and max function in R.  min function in R – min(), is used to calculate the minimum of vector elements or minimum of a particular column of a dataframe. minimum of a group can also calculated using min() function in R by providing it inside the aggregate function. max(), is used to calculate the maximum of vector elements or maximum of a particular column of a dataframe. maximum of a group can also calculated using max() function in R by providing it inside the aggregate function. row wise maximum and minimum is calculated using max() and min() function with the help of dplyr package. we also calculates column wise maximum and column wise minimum. lets see an example of each.

  • min() function in R computes the minimum value of a vector or data frame.
  • max() function in R computes the maximum value of a vector or data frame.
  • column wise maximum and minimum of the dataframe using max() and min() function.
  • Row wise maximum and minimum of the dataframe in R using max() and min() function.
  • maximum and minimum value of the group is calculated using max() and min() along with aggregate() and dplyr packages.

 

Syntax for min and Max function in R:

min(x, na.rm = FALSE)

max(x, na.rm = FALSE)

  •   x – is numeric or character vector
  • na.rm – a logical indicating whether missing values should be removed.

 

Example of Max function in R:

# max function in R for a numeric vector

x <-c(1.234,2.342,-4.562,5.671,12.345,-14.567)
max(x)
output:
[1] 12.345

 

Example of Max function in R with NA:

Max function doesn’t give desired output, If NAs are present in the vector. So it has to be handled by using na.rm=TRUE in max() function

# max function in R for a numeric vector

x <-c(1.234,2.342,-4.562,5.671,12.345,-14.567,NA)
max(x, na.rm=TRUE)
output:
[1] 12.345

 

Example of Max function in R with character vector:

# max function in R for a character vector

y<-c("d","e","a","x")
max(y)
output:
[1] “x”

 

Example of max() function in R dataframe: 

Lets create the data frame to demonstrate max function – max() in r

### create the dataframe 
my_basket = data.frame(ITEM_GROUP = c("Fruit","Fruit","Fruit","Fruit","Fruit","Vegetable","Vegetable","Vegetable","Vegetable","Dairy","Dairy","Dairy","Dairy","Dairy"), 
                       ITEM_NAME = c("Apple","Banana","Orange","Mango","Papaya","Carrot","Potato","Brinjal","Raddish","Milk","Curd","Cheese","Milk","Paneer"),
                       Price = c(100,80,80,90,65,70,60,70,25,60,40,35,50,120),
                       Tax = c(2,4,5,6,2,3,5,1,3,4,5,6,4,3))
my_basket

so the resultant dataframe will be

sum function in R 1

 

maximum value of a column in R data frame using max() function :

Max() function in R 1

max() function takes the column name as argument and calculates the maximum value  of that particular column

# max() function in R : maximum value of a column in data frame

max(my_basket$Price)

so the resultant maximum value of the “Price” column will be

output:

[1] 120

 

column wise maximum using max() function:

max() function is applied to the required column through mapply() function, so that it  calculates the maximum value of required columns as shown below.

 
# max() function in R : maximum value of multiple columns in data frame 

 mapply(max,my_basket[,c(-1,-2)])

so the resultant maximum value of “Price” and “Tax” columns will be

max and min function in R 6

 

Maximum value of the column by group using max() function

aggregate() function along with the max() function calculates the maximum value of a group. here maximum of “Price” column, for “Item_Group” is calculated.

##### maximum value of the column by group 
aggregate(x= my_basket$Price,
          by= list(my_basket$ITEM_GROUP),
          FUN=max)

Item_group has three groups “Dairy”,”Fruit” & “Vegetable”. maximum price for each group is calculated as shown below

max and min function in R 2

 

Row wise maximum using max() function along with dplyr

Row wise maximum is calculated with the help rowwise() function of dplyr package  and max() function as shown below

## row wise max using dplyr 
library(dplyr)

my_basket %>%
  rowwise() %>%
  mutate(
    Total_price = max(c(Price,Tax))
  )

row wise max of “Price” and “Tax” is calculated and  populated for each row as shown below

max and min function in R 3

 

 

 

Example of Min function in R:

# min function in R for a numeric vector

x <-c(1.234,2.342,-4.562,5.671,12.345,-14.567)
min(x)
output:
[1] -14.567

 

Example of Min function in R with NA:

Min function doesn’t give desired output, If NAs are present in the vector. so it has to be handled by using na.rm=TRUE in min() function

# min function in R for a numeric vector

x <-c(1.234,2.342,-4.562,5.671,12.345,-14.567,NA)
min(x, na.rm=TRUE)
output:
[1] -14.567

 

Example of Min function in R with character vector:

# min function in R for a character vector

y<-c("d","e","a","x")
min(y)
output:
[1] “a”

 

Example of min() function in R dataframe: 

Lets create the data frame to demonstrate min function – min() in r

### create the dataframe 
my_basket = data.frame(ITEM_GROUP = c("Fruit","Fruit","Fruit","Fruit","Fruit","Vegetable","Vegetable","Vegetable","Vegetable","Dairy","Dairy","Dairy","Dairy","Dairy"), 
                       ITEM_NAME = c("Apple","Banana","Orange","Mango","Papaya","Carrot","Potato","Brinjal","Raddish","Milk","Curd","Cheese","Milk","Paneer"),
                       Price = c(100,80,80,90,65,70,60,70,25,60,40,35,50,120),
                       Tax = c(2,4,5,6,2,3,5,1,3,4,5,6,4,3))
my_basket

so the resultant dataframe will be

sum function in R 1

minimum value of a column in R data frame using min() function :

min() function takes the column name as argument and calculates the minimum value  of that particular column

# min() function in R : minimum value of a column in data frame

min(my_basket$Price)

so the resultant minimum value of the “Price” column will be

output:

[1] 25

 

column wise minimum using min() function:

Max() function in R 2

min() function is applied to the required column through mapply() function, so that it  calculates the minimum value of required columns as shown below.

 
# min() function in R : minimum value of multiple columns in data frame 

mapply(min,my_basket[,c(-1,-2)])

so the resultant minimum value of “Price” and “Tax” columns will be

max and min function in R 4

 

Minimum value of the column by group using min() function

aggregate() function along with the min() function calculates the minimum value of a group. here minimum of “Price” column, for “Item_Group” is calculated.


##### minimum value of the column by group 
aggregate(x= my_basket$Price, 
          by= list(my_basket$ITEM_GROUP), 
          FUN=min)

Item_group has three groups “Dairy”,”Fruit” & “Vegetable”. minimum price for each group is calculated as shown below

max and min function in R 5b

 

Row wise minimum using min() function along with dplyr

Row wise minimum is calculated with the help rowwise() function of dplyr package  and min() function as shown below


## row wise min using dplyr

library(dplyr) 

my_basket %>%
  rowwise() %>% 
  mutate( 
    min_price = min(c(Price,Tax)) 
    )

row wise min of “Price” and “Tax” is calculated and  populated for each row as shown below

max and min function in R 5

For further understanding of min() and max() function in R using dplyr one can refer the dplyr documentation


Other Related Topics:

 

previous small min and max function in r                                                                                                          next small min and max function in r

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.