What is the range() Function in R

The range() function in R is used to return a vector with two elements:

  1. The first item represents the minimum value of the input vector.
  2. The second item represents the maximum value of the input vector.

Visualization of range() Function in R

Syntax

range(…, na.rm = FALSE)

Parameters

  1. …: It represents any numeric or character objects or vectors.
  2. 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

Passing na.rm = false to the range() function

# 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

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!

Leave a Comment