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?
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).
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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 –
- Average of Values in an R Vector
- R – Check If All Elements in a Vector are Equal
- Calculate Variance of a Vector in R
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.