R - range() Function
The R range() function returns a vector containing the minimum and maximum of all the given arguments. The syntax for using this function is given below:
Syntax
range(..., na.rm = FALSE, finite = FALSE)
Parameters
... |
Required. Specify any numeric or character objects. |
na.rm |
Optional. Specify TRUE to remove NA or NaN values before the computation. Default is FALSE. |
finite |
Optional. Specify TRUE to remove Inf, -Inf, NA or NaN values before the computation. Default is FALSE. |
Return Value
Returns a vector containing the minimum and maximum of all the given arguments.
Example:
The example below shows the usage of range() function.
v <- c(10, 15, 20, 25, 30, 35, NA, NaN, Inf, -Inf) cat("The vector contains:\n") cat(v, "\n\n"); cat("range(v): ", range(v), "\n") cat("range(v, na.rm=TRUE): ", range(v, na.rm=TRUE), "\n") cat("range(v, finite=TRUE): ", range(v, finite=TRUE), "\n")
The output of the above code will be:
The vector contains: 10 15 20 25 30 35 NA NaN Inf -Inf range(v): NA NA range(v, na.rm=TRUE): -Inf Inf range(v, finite=TRUE): 10 35
Example:
Consider one more example where the range() function is used with matrix.
m <- matrix(c(10, 50, -10, 100, NA, NaN, Inf, -Inf), nrow=2) cat("The matrix contains:\n") print(m); cat("\n"); cat("range(m): ", range(m), "\n") cat("range(m, na.rm=TRUE): ", range(m, na.rm=TRUE), "\n") cat("range(m, finite=TRUE): ", range(m, finite=TRUE), "\n")
The output of the above code will be:
The matrix contains: [,1] [,2] [,3] [,4] [1,] 10 -10 NA Inf [2,] 50 100 NaN -Inf range(m): NA NA range(m, na.rm=TRUE): -Inf Inf range(m, finite=TRUE): -10 100
❮ R Statistical Functions