min() function
min() Function in R Language
The min() function in R is used to determine the smallest value within an object. This object can be a vector, list, matrix, data frame, or other types.
Syntax
min(object, na.rm)
Parameters
- object: A vector, matrix, list, data frame, etc., containing the elements.
- na.rm: A logical parameter; if
TRUE, it removesNAvalues before computing the minimum.
Example 1: Finding the Minimum Value in Vectors
# R program to demonstrate the min() function
# Creating vectors
vec1 <- c(3, 7, 1, 5, 9)
vec2 <- c(10, NA, 2, 6, 15)
# Applying min() function
min(vec1)
min(vec2, na.rm = FALSE)
min(vec2, na.rm = TRUE)
Output:
[1] 1
[1] NA
[1] 2
Example 2: Finding the Minimum Value in a Matrix
# R program to demonstrate the min() function
# Creating a matrix
mat <- matrix(10:21, nrow = 3, byrow = TRUE)
print(mat)
# Applying min() function
min(mat)
Output:
[,1] [,2] [,3] [,4]
[1,] 10 11 12 13
[2,] 14 15 16 17
[3,] 18 19 20 21
[1] 10
Leave a Reply