Get the Minimum and Maximum element of a Vector in R Programming in detail
The range() function in R is used to determine the smallest (minimum) and largest (maximum) values in a given vector.
Syntax:
range(x, na.rm = FALSE, finite = FALSE)
Parameters
- x: A numeric vector whose minimum and maximum values are to be calculated.
- na.rm: Logical (Boolean) value indicating whether to exclude
NAvalues from the computation. Default isFALSE. - finite: Logical value to specify whether to exclude non-finite elements (like
Infand-Inf). Default isFALSE.
Examples of range() Function in R
Example 1: Finding the Minimum and Maximum of a Numeric Vector
# Define a numeric vector
vec <- c(10, 4, 7, 15, 2, 6)
# Compute the range
range(vec)
Output:
[1] 2 15
Example 2: Handling NA and Non-Finite Values
# Define a numeric vector with NA and non-finite values
vec <- c(10, 4, NA, Inf, 7, -Inf, 2)
# Compute the range without additional arguments
range(vec)
# Compute the range, ignoring NA values
range(vec, na.rm = TRUE)
# Compute the range, ignoring NA and non-finite values
range(vec, na.rm = TRUE, finite = TRUE)
Output:
[1] NA NA
[1] -Inf Inf
[1] 2 10
Leave a Reply