range Function in R (2 Examples)


This tutorial shows how to get the minimum and maximum of a data object using the range function in R.

Table of contents:

Here’s the step-by-step process:


Example 1: Apply range() Function to Numeric Vector

The following R code demonstrates how to use the range function to find the largest and smallest number in a numeric or integer vector.

For this, we first have to create some example data:

x_num <- 1:10                 # Create numeric vector
x_num                         # Print numeric vector
#  [1]  1  2  3  4  5  6  7  8  9 10

The previous output shows the structure of our example data: We have created a numeric vector object containing ten vector elements.

Next, we can apply the range function as shown below:

range(x_num)                  # Apply range function
# [1]  1 10

The RStudio console returns the values 1 and 10, i.e. the minimum and maximum values of our vector.


Example 2: Apply range() Function to Character Vector

In Example 2, I’ll demonstrate how to apply the range function to a character vector.

Let’s create some example data:

x_cha <- letters[1:10]        # Create character vector
x_cha                         # Print character vector
#  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

Now, we can apply the range command to these data as shown in the following R code:

range(x_cha)                  # Apply range function
# [1] "a" "j"

As you can see, the range function has returned the alphabetically first and last letters of our vector.


Video & Further Resources

Do you want to know more about the application of the range function? Then you may have a look at the following video on my YouTube channel. I’m explaining the topics of the present article in the video:



Furthermore, you might have a look at the related tutorials on this website.


In summary: You have learned in this tutorial how to apply the range function to identify the interval of values in the R programming language.

Please note that the range function provides more arguments as I have shown in this tutorial. The range function can also handle NA values (i.e. missing data) using the na.rm argument, or infinite values using the finite argument.

Have a look at the help documentation of the range function by typing and executing ?range in the RStudio console for more details.

Let me know in the comments section below, in case you have additional questions.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.