R has an efficient way to get the minimum and maximum values within a vector: the range() function. The range is the interval or difference between the lowest and the highest value within the data vector. This has numerous practical uses when analyzing a data frame and is required by many statistical control and graph functions (if you are writing packages or helper utilities).
The range is also required when building and validating statistical process control models, since you will want to ensure the model is accurate and relevant for the full range, interval, or difference of possible values in the dataset or dataframe.
Finding the Minimum Value and Maximum Value
Within R, the range function uses the following syntax: range (vector)
The implementation of this R chart function is extremely efficient and runs within Base R. That being said, the range function requires iteration across the full numeric vector when called, so incorporate this consideration into your design.
The Range Function in R
In R, the range function has the format of range(vector) and it produces the smallest and largest values of the numeric vector that is being evaluated. The result is that you have the range chart of values covered by the data set.
# range in R > x=c(5,2,7,9,4) > range(x)  2 9
If you examine the example, you will see the results accurately captured the range of the vector: 2 for the minimum, 9 for the maximum.
Range in R – Missing values option
When dealing with the NA value range in R has a logical option in the form of na.rm. The na.rm parameter, which means NA remove, can be TRUE or FALSE. This helps protect you from missing value errors. If the logical option is FALSE, which it is by default if omitted, the function returns an NA value for both the minimum value and maximum value. If it is TRUE, then, NA values are discounted.
# range in R - the NA issue > x=c(5,2,NA,9,4) > range(x,na.rm=FALSE)  NA NA
Here, we have the case where na.rm is FALSE. Note that both resulting values are NA, this indicates that there is no answer.
# range in r - using na.rm to clean up results > range(x,na.rm=TRUE)  2 9
Here, na.rm is TRUE and the NA value is ignored resulting in a minimum and maximum values.
Range in R – Character data
Here, we have the case of applying the range function to a character vector. When applied to characters, a range analysis returns the first and last of the characters in alphabetical order- the minimum value is the first character vector on the list, and the maximum value is the last variable on the list, alphabetically.
# range in R - vectors with alphabetical data > x=c("c","r","e","a","g","e","r") > range(x)  "a" "r"
In this example, there are 2 e’s and 2 r’s, however, because duplications of multiple variables do not matter it has no effect on the summary statistics.
Finding the range of a data set or dataframe effectively gives you its boundaries. The range () function in R provides you with the visible extreme number at the max and min of the series. Note that these may not necessarily represent the real world limits of a series if your sample size or subgroup size is small and / or extreme values are very rare. Most lottery ticket samples have payouts ranging between $0 and $100; a tiny handful of potential samples have payouts massively above that….