Find range in a vector using min and max functions
We can find the range by performing the difference between the minimum value in the vector and the maximum value in the given vector. We can find the maximum value using the max() function and the minimum value by using the min() function.
Syntax:
max(vector)-min(vector)
If a vector contains NA values then we should use the na.rm function to exclude NA values
Example:
R
# create vector data = c (12, 45, NA , NA , 67, 23, 45, 78, NA , 89) # display print (data) # find range print ( max (data, na.rm= TRUE )- min (data, na.rm= TRUE )) |
Output:
[1] 12 45 NA NA 67 23 45 78 NA 89 [1] 77
How to Find the Range in R?
In this article, we will discuss how to find the Range in R Programming Language.
The range can be defined as the difference between the maximum and minimum elements in the given data, the data can be a vector or a dataframe. So we can define the range as the difference between maximum_value – minimum_value