R Tutorial R Charts & Graphs R Statistics R References

R - min() Function



The R min() function returns smallest of all elements present in the argument. The syntax for using this function is given below:

Syntax

min(x1, x2, ..., na.rm = FALSE)

Parameters

x1, x2, ... Required. Specify numeric vectors.
na.rm Optional. Specify TRUE to remove NA or NaN values before the computation. Default is FALSE.

Return Value

Returns smallest of all elements present in the argument.

Example:

The example below shows the usage of min() function.

v1 <- c(10, 55, 25, -56, 30, 99)
v2 <- c(1, -2, 30, 4, -50)
cat("Smallest element of v1:", min(v1), "\n")
cat("Smallest element of v2:", min(v2), "\n")
cat("Smallest element of v1 and v2:", min(v1, v2), "\n")

m <- matrix(c(10, 20, 30, 40, 50, 60), ncol=2)
cat("\nThe matrix contains:\n")
print(m)
cat("Smallest element of matrix:", min(m))
cat("\nSmallest element along first column:", min(m[,1]))

The output of the above code will be:

Smallest element of v1: -56 
Smallest element of v2: -50 
Smallest element of v1 and v2: -56 

The matrix contains:
     [,1] [,2]
[1,]   10   40
[2,]   20   50
[3,]   30   60
Smallest element of matrix: 10
Smallest element along first column: 10

Using na.rm parameter

The na.rm parameter can be set TRUE to remove NA or NaN values before the computation.

Example:

Consider the example below to see the usage of na.rm parameter.

v1 <- c(10, 20, 30, NA)
v2 <- c(10, 20, 30, NaN)

cat("Smallest element of v1:", min(v1), "\n")
cat("Smallest element after removing NA:", min(v1, na.rm=TRUE), "\n")

cat("\nSmallest element of v2:", min(v2), "\n")
cat("Smallest element after removing NaN:", min(v2, na.rm=TRUE), "\n")

The output of the above code will be:

Smallest element of v1: NA 
Smallest element after removing NA: 10 

Smallest element of v2: NaN 
Smallest element after removing NaN: 10 

❮ R Statistical Functions

5