min() in R
The min() is a built-in R function that takes an object as an input and returns the minimum value out of it. To find the minimum value of vector elements, data frame, and columns, use the min() function.
min(input, na.rm = FALSE)
na.rm: It removes the NA values; if it mentioned FALSE, it considers NA, or if it mentioned TRUE, it removes NA from the vector or a data frame.
The min() function returns the minimum value of a vector or data frame.
Applying min() function to Vector in R
Define a vector and pass that vector to the min() function to find the minimum element of the vector. You can define a vector using the c() function.
data <- c(19, 21, -20, -46, 29) cat("The minimum value of thev vector is:", min(data))
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.
Applying 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))
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 min() function.
data <- c(19, 21, -20, NA, 29) cat("The minimum value of thev vector is:", min(data, na.rm = TRUE))
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.
Applying min() function to a data set
Let’s use the ChickWeight dataset and check out its first 6 rows using the head() function.
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
If we want 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)
The minimum value of weight column is: 35
That means the minimum weight of the ChickWeight dataset is 35.
Minimum value across all columns
You can find the minimum value of all the columns of your data matrix by using the sapply() function. The sapply() function works like lapply(), but it tries to interpret the output to the most fundamental data structure possible, either Vector or Matrix.
Let’s use the mtcars dataset and find the maximum value across all the columns.
data(mtcars) sapply(mtcars, min)
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
Find 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))
Find 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.
In our example, the minimum value of mtcars data frame is 0.
Find 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, ])
Find the minimum character of 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)
 "At HBOMAX"
That is it for the R min() function.
Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.