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.
range(…, na.rm = FALSE)
- …: 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.
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)
 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)
 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))
 5 5  "After converting a range object to list" []  5 5