In our previous blogs, we have gone through functions like mean, median, and mode. Today, we will be looking into min and max functions in R to find the minimum and maximum values in R. We can use these functions with a vector and a Dataframe as well.

Syntax of min and max function in R
Min and Max: The min and max functions in r is used to find the minimum and maximum values present in the data.
min(data, na.rm = F)
max(data, na.rm = F)
Where,
Data = The input data.
na.rm = Mention true to remove NA values in the data.
A simple example of min and max functions
In this section, we will be looking into the working of min and max functions in r programming. Let’s start with a simple example.
#Vector with values df <- c(23,43,45,65,45,34,56,76,56)
#Returns minimum value of the vector min(df)
23
#Returns maximum value of the vector max(df)
76
Yes, as simple as that. You have to pass the vector to these functions to get the both highest and lowest values present in the vector.
Working with the dataframe
Let’s use these functions to get the min and max values present in the dataframe. Let’s use the iris dataset for this purpose. Import the data and get the things started.
#Imports the data df <- datasets::iris #Display data df
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
7 4.6 3.4 1.4 0.3 setosa
8 5.0 3.4 1.5 0.2 setosa
9 4.4 2.9 1.4 0.2 setosa
10 4.9 3.1 1.5 0.1 setosa
The data is ready now. Let’s roll!!!
min(df$Sepal.Length)
4.3
min(df$Petal.Length)
1
max(df$Sepal.Length)
7.9
max(df$Petal.Length)
6.9
That’s fantastic!. We got both the minimum and maximum values of the specific columns present in the dataset of iris.
Min and max in R with NA values
Many times, the data will contain NA values. To deal with them we can use the na.rm parameter to negate them.
Let’s see how it works.
#Vector df <- c(23,54,67,54,NA,45,NA,67,NA,NA,34,0,76) #Returns min value min(df) #Returns max value max(df)
NA
NA
You can see the errors. Let’s use na.rm function to negate them.
min(df, na.rm = T) max(df, na.rm = T)
0
76
Wrapping Up
The min and max functions in R are used to get the minimum and maximum values present in the input data. You can go through this article and practice on your own to get the better of this topic. Happy R!!!