What is the min() Function in R

The min() is a built-in R function that finds the minimum value of an object, such as a vector, a list, a matrix, or a data frame12. The syntax of the min() function is min(input, na.rm = FALSE), where input is the object you want to find the minimum value of, and na.rm is a logical argument that suggests whether missing values should be removed or not.

Syntax

min(input, na.rm = FALSE)

Parameters

input: It is a  Vector or a data frame.

na.rm: It removes the NA values; if it mentions FALSE, it considers NA, or if it mentioned TRUE, it removes NA from the vector or a data frame.

Return value

The min() function returns the minimum value of a vector or data frame.

 Example 1: How to find a minimum value of a vector

To find the minimum value of a vector in R, you can use the min() function with the vector as the input argument. For example, x <- c(1, 10, 2, 4, 100, 3) will return 1 as the minimum value of x.

data <- c(19, 21, -20, -46, 29)
cat("The minimum value of thev vector is:", min(data))

Output

The minimum value of thev vector is: -46

You can see from the vector that mathematically, the minimum value is -46, and we get the same output.

Example 2: Applying the min() function to a Vector containing NA values

If you add the NA value as missing data to the vector, it will return the NA value as a minimum value because it is not what the NA value is because it can be anything. So it will assume the minimum value as NA returns that in the output.

data <- c(19, 21, -20, NA, 29)
cat("The minimum value of thev vector is:", min(data))

Output

The minimum value of thev vector is: NA

But don’t worry, there is an easy solution! Just specify the option na.rm = TRUE within the min() function.

data <- c(19, 21, -20, NA, 29)
cat("The minimum value of thev vector is:", min(data, na.rm = TRUE))

Output

The minimum value of thev vector is: -20

You can see that it has completely ignored the NA value, and it returns -20 as the minimum value.

Example 3: Applying the min() function to a data set

Let’s use the ChickWeight dataset and check out its first 6 rows using the head() function.

data(ChickWeight)
head(ChickWeight)

Output

   weight Time Chick Diet
1    42     0    1    1
2    51     2    1    1
3    59     4    1    1
4    64     6    1    1
5    76     8    1    1
6    93    10    1    1

To calculate the maximum and minimum of one column, we can apply the max and min functions to that specific column with the name of the data, the $-sign, and the column’s name.

Let’s say we want to calculate the minimum value of the weight column from the whole dataset, and then you can use the min() function and pass the weight column.

data(ChickWeight)
val <- min(ChickWeight$weight)
cat("The minimum value of weight column is:", val)

Output

The minimum value of weight column is: 35

That means the minimum weight of the ChickWeight dataset is 35.

Example 4: How to find a minimum value across all columns

You can find the minimum value of all the columns of your data matrix using the sapply() function. The sapply() function works like lapply() but tries to interpret the output to the most fundamental data structure possible, either vector or matrix.

data(mtcars)
sapply(mtcars, min)

Output

  mpg   cyl   disp    hp    drat   wt   qsec    vs   am    gear  carb
10.400 4.000 71.100 52.000 2.760 1.513 14.500 0.000 0.000 3.000  1.000

Example 5: Finding the minimum value between two columns.

We will use the mtcars dataset and find the minimum value between two columns. For example, we will find the minimum value of the columns mpg and cyl of mtcars.

data(mtcars)
min(c(mtcars$mpg, mtcars$cyl))

Output

[1] 4

Example 6: Finding the global minimum value of the data frame.

The computation of the global minimum value of a data frame is straightforward. Just apply the min() function and pass the data frame as an argument that returns the min value.

data(mtcars)
min(mtcars)

Output

[1] 0

In our example, the minimum value of the mtcars data frame is 0.

Example 7: Finding the Minimum of the row using the min() Function.

You can find the minimum value of the data frame row using the following code.

data(mtcars)
min(mtcars[10, ])

Output

[1] 0

Example 8: Find the minimum character of the String

We can use the min() function to define the minimum of strings in alphabetic order. 

data <- c("Harry Potter",
 "TV Show is Happening",
 "At HBOMAX",
 "It is in early stage")

min(data)

Output

[1] "At HBOMAX"

That is it for the R min() function.

Leave a Comment