R has an efficient way to get the minimum and maximum values within a vector: the range() function. The range is the interval 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 and graphics functions (if you are writing packages or helper utilities).

The range is also required when building and validating statistical models, since you will want to ensure the model is accurate and relevant for the full range of possible values.

## Finding the Minimum Value and Maximum Value

Within R, the range function uses the following syntax: range (vector)

The implementation of this function is extremely efficient and runs within Base R. That being said, the range function requires iteration across the full 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 vector that is being evaluated. The result is that you have the range of values covered by the data set.

```
# range in R
> x=c(5,2,7,9,4)
> range(x)
[1] 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. If the logical option is FALSE, which it is by default if omitted, the function returns an NA value for both the minimum and maximum. 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)
[1] 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)
[1] 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, range returns the first and last of the characters in alphabetical order.

```
# range in R - vectors with alphabetical data
> x=c("c","r","e","a","g","e","r")
> range(x)
[1] "a" "r"
```

In this example, there are 2 e’s and 2 r’s, however, because duplications do not matter it has no effect on the results.

Finding the range of a data set effectively gives you its boundaries. The range () function in R provides you with the visible extremes of the series. Note that these may not necessarily represent the real world limits of a series if your sample 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….