Open In App

Get the Minimum and Maximum element of a Vector in R Programming – range() Function

Last Updated : 16 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

range() function in R Programming Language is used to get the minimum and maximum values of the vector passed to it as an argument.

Syntax: range(x, na.rm, finite)

Parameters: 

  • x: Numeric Vector
  • na.rm: Boolean value to remove NA
  • finite: Boolean value to exclude non-finite elements

R – range() Function Examples

Example 1: Get the Minimum and Maximum element of a Vector using range() Function in R language

R




# R program to find the
# minimum and maximum element of a vector
 
# Creating a vector
x <- c(8, 2, 5, 4, 9, 6, 54, 18)
 
# Calling range() function
range(x)


Output: 

[1]  2 54

Example 2: Using range() Function in R programming with NA 

R




# R program to find the
# minimum and maximum element of a vector
 
# Creating a vector
x <- c(8, 2, Inf, 5, 4, NA, 9, 54, 18)
 
# Calling range() function
range(x)
 
# Calling range() function
# excluding NA values
range(x, na.rm = TRUE)
 
# Calling range() function
# excluding finite values
range(x, na.rm = TRUE, finite = TRUE)


Output: 

[1] NA NA
[1]   2 Inf
[1]  2 54

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads