Range of values in R
The range
function is used to find the range of a set of numeric or character values, this is, the minimum and maximum values of a numeric vector or the first and last element of a character vector ordered in alphabetical order.
Syntax
The range
function in R has the syntax described below:
range(..., # Numeric or character object
na.rm = FALSE, # If TRUE, removes missing values
finite = FALSE) # If TRUE, removes non-finite values
This function returns a vector with two elements, where the first element is the minimum value and the second element is the maximum value.
Range of numeric vector
Given a numeric vector named x
you can obtain its range passing the object to the range
function.
# Sample numeric vector
x <- c(10, 1, 14, -5, 4)
# Range of 'x'
range(x)
# Equivalent to:
# c(min(x), max(x))
-5 14
The previous code return a two-element vector with -5 (the minimum value) and 14 (the maximum value).
However, if the vector contains NA
values the function will return NA NA
, as illustrated below.
# Sample numeric vector with a missing value
x <- c(10, 1, 14, NA, -5, 4)
# Range of 'x'
range(x)
NA NA
To solve this issue you will need to set na.rm = TRUE
, so missing values will be omitted before the computation.
# Sample numeric vector with a missing value
x <- c(10, 1, 14, NA, -5, 4)
# Range of 'x'
range(x, na.rm = TRUE)
-5 14
The sample vector can also contain non-finite values, such as Inf
. In this scenario, the function will take into account these values.
# Sample numeric vector with an Inf value
x <- c(10, 1, 14, Inf, -5, 4)
# Range of 'x'
range(x)
-5 Inf
To remove non-finite values you can set finite = TRUE
, so only finite values will be taken into account.
# Sample numeric vector with an Inf value
x <- c(10, 1, 14, Inf, -5, 4)
# Range of 'x'
range(x, finite = TRUE)
-5 14
Range of character vector
The range
function also support character vectors. In this case the function will order the values alphabetically and return the first and last element, as illustrated in the example below.
# Sample character vector
y <- c("and", "sand", "band")
# Range of 'y'
range(y)
"and" "sand"
The function returns "and"
and "sand"
, as these are the first and last element if y
is order alphabetically.
Range of a whole data frame or matrix
If a data frame (with homogeneous data types) or matrix is passed as input of range
the function will return the range of the values of the whole object.
# Sample data frame
df <- data.frame(x = c(0, 1, 3), y = c(5, 2, 4))
# Range of 'df'
range(df)
0 5
The output is 0 5
, as 0 and 5 are the minimum and maximum values between all columns, respectively.
Range for each column of a data frame or matrix
If you want to calculate the range for each column of a data frame or matrix you will need to utilize the apply
function as shown in the example below.
# Sample data frame
df <- data.frame(x = c(0, 1, 3), y = c(5, 2, 4))
# Range of each column of 'df'
apply(df, 2, range)
x y
[1,] 0 2
[2,] 3 5
The range of the column named x
is 0 3
and the range of the column named y
is 2 5
.
The extendrange
function
The extendrange
function extends the range of a numeric vector by a small percentage on both sides. The default percentage is 0.05.
# Sample numeric vector
x <- c(10, 1, 14, -5, 4)
# Extended range of 'x'
extendrange(x)
-5.95 14.95
In the previous example the range is extended 0.95 because the difference between the maximum and minimum value is 19 and 19 x 0.05 = 0.95.
The fraction can be customized with f
, as shown below.
# Sample numeric vector
x <- c(10, 1, 14, -5, 4)
# Extended range of 'x'
extendrange(x, f = 0.01)
-5.19 14.19
In addition, the extended range can be customized for each side, passing a vector with two percentages, being the first the one used on the left and the second the one used on the right.
# Sample numeric vector
x <- c(10, 1, 14, -5, 4)
# Extended range of 'x'
extendrange(x, f = c(0.01, 0.05))
-5.19 14.95