get the range of a vector in R

Get Range of a Vector in R (With Examples)

In this tutorial, we will look at how to find the range of a vector in R with the help of some examples.

How do you find the range of a vector in R?

get the range of a vector in R

You can use the built-in range() function to calculate the range of values in a vector in R. Alternatively, you can also use the R max() and min() functions.

Let’s now look at the two methods in detail with the help of some examples.

Method 1 – Using the range() function

Use the following steps to compute the range of a vector using the range() function.

  • Step 1 – Pass the vector to the range() function and get the range vector. The range vector contains the min and max values from the original vector.
  • Step 2 – Take the difference between the max and the min values in the range vector to get the range.

Let’s look at an example.

Here, we will calculate the range of an integer vector containing values from 1 to 4.

# create a vector
vec <- c(1, 2, 3, 4)
# get the range vector
range_vec = range(vec)
# display the range vector
print(range_vec)
# get the range
print(range_vec[2] - range_vec[1])

Output:

[1] 1 4
[1] 3

Here, we first print the range vector and then compute the range. We get 3 as the range since 3 is the difference between the largest and the smallest value in the above vector (4 – 1 = 3).

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

What if our vector has NA values?

Let’s find out.

This time we will pass a vector with some NA values to the range() function.

# create a vector with NA values
vec <- c(1, 2, NA, 3, 4, NA)
# get the range vector
range_vec = range(vec)
# display the range vector
print(range_vec)
# get the range
print(range_vec[2] - range_vec[1])

Output:

[1] NA NA
[1] NA

We get NA as the output. This is because any arithmetic operation with NA results in an NA in R.

To get the range of a vector with NA values, pass na.rm=TRUE as an argument to the range() function.

# create a vector with NA values
vec <- c(1, 2, NA, 3, 4, NA)
# get the range vector
range_vec = range(vec, na.rm = TRUE)
# display the range vector
print(range_vec)
# get the range
print(range_vec[2] - range_vec[1])

Output:

[1] 1 4
[1] 3

We now get the range of the above vector as 3.

Method 2 – Using the min() and max() functions

Alternatively, you can also use a combination of the min() and the max() functions in R to compute the range of a vector. Use the following steps –

  • Step 1 – Calculate the maximum value in the vector using the max() function.
  • Step 2 – Calculate the minimum value in the vector using the min() function.
  • Step 3 – Take the difference between the maximum and the minimum to get the range of the vector.

Let’s look at an example. We will use the same vector as above.

# create a vector
vec <- c(1, 2, 3, 4)
# get the range of the vector
print(max(vec)-min(vec))

Output:

[1] 3

We get the range as 3.

Now, what if the vector has NA values?

Just like we got NA from the range() function, the max() and min() functions will also give NA if the vector contains NA value(s). To mitigate this, pass na.rm=TRUE to the max() and the min() functions.

# create a vector with NA values
vec <- c(1, 2, NA, 3, 4, NA)
# get the range of the vector
print(max(vec, na.rm = TRUE) - min(vec, na.rm = TRUE))

Output:

[1] 3

We get the same range as above. Here, we compute the max and the min by excluding the NA values and then take their difference to get the range.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Authors

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

  • Gottumukkala Sravan Kumar
Scroll to Top