The range() function in R is used to return a vector with two elements:
- The first item represents the minimum value of the input vector.
- The second item represents the maximum value of the input vector.
Syntax
range(…, na.rm = FALSE)
Parameters
- …: It represents any numeric or character objects or vectors.
- na.rm: It takes a Boolean value (TRUE or FALSE), suggesting that the NaN (Not a Number) values should be omitted.
Return value
The range() method returns a vector object holding the result.
Example 1: How to use the range() function
# Creating an input vector object
rv <- c(11, 21, 31, 41, 50, NaN)
# Calling the range() method
range(rv, na.rm = TRUE)
Output
[1] 11 50
Example 2: Passing na.rm = false
# Creating an input vector object
a <- c(11, 21, 31, 41, 50, NaN)
# Calling the range() method
range(a, na.rm = FALSE)
Output
[1] NaN NaN
Example 3: Converting a range object to a list
You can convert a range object to a list using a range() method.
data <- range(5)
print(data)
print("After converting a range object to list")
print(list(data))
Output
[1] 5 5
[1] "After converting a range object to list"
[[1]]
[1] 5 5
That’s it!

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.