Range function in R


April 5, 2022, Learn eTutorial
2739

In this tutorial you will learn how to use range function in R. In R programming language range function is used for several practical implementations that require analysis of data using statistical and graph functions. You will also learn how to use range function with NA values and Inf values on vector or dataframe.

What is Range in R?

Statistically, a range of a data set is defined as the difference between maximum and minimum values i.e  ( max_value - min_value). 

In R programming, a range () function is defined as the interval between the largest (maximum) and smallest (minimum) data value within a vector or column in a data frame in R.

Although the definitions appear to be identical, the range function in R programming differs in a way that it only returns the two extremes rather than the difference value. So a range() in R is suitable for the following:-

  • To find the lowest and highest values of the given vector
  • To find the lowest and highest values of the column in a data frame

How to declare a range in R?

A range() function in R source code is declared by using the following syntax.

The syntax for range function in R :


range(x,na.rm = FALSE)

Where

  • x can be a character or a numeric vector.
  • na.rm: is an optional parameter which controls the elimination of NA values prior determining the range.

Let us begin with simple practical examples for better understanding. First, create a vector x of numeric data type which holds a set of values.


#Created a vector of numeric data type using c()
x = c(24,5,6,7,7,-1,0)
cat("The numeric vector x is :",x,"\n")

The R code after execution returns a vector x as


The numeric vector x is : 24 5 6 7 7 -1 0

Now by applying the range() function in vector x you can find the maximum(24) and minimum(-1)values from the vector x.


range(x)

The lowest and highest values are returned in x.


[1] -1 24

Example of using range () with NA

NA is a reserved word that indicates the value is not available or missing ie “ Not Available (NA)” We need to change the syntax by adding a parameter na.rm to the range() whose value can be either TRUE or FALSE. In case NA is present in an input vector or data structure the range function fails to return the result. So na.rm assigned to TRUE will remove the NA present in input while finding the range() function and providing the accurate result.

Consider the same vector x with NA occupied among a given set of values together with Inf which is another reserved word in R to show infinity.


x = c(24,5,Inf,6,7,7,-1,0,NA)

Applying range(x) we get


[1] NA NA

From the output, it is clearly visible that the input with NA is not evaluated by a range(). Here we go with the modified syntax for removing NA.


range(x,na.rm=TRUE)

The output after addition of na.rm=TRUE inside range() returns lowest value(-1) and highest value(Inf),which excludes the NA among the vector values.;


[1]  -1 Inf

Example of range() using Dataframe

Let us begin by creating a data frame with a few items of different categories and find out the lowest and highest item in terms of their price.


items = data.frame(ITEMS = c("Fruit","Fruit","Fruit","Fruit","Fruit","Vegetable","Vegetable","Vegetable","Vegetable","Dairy","Dairy","Dairy","Dairy","Dairy"), 
                   ITEM_NAME = c("Apple","Banana","Orange","Mango","Papaya","Carrot","Potato","Brinjal","Raddish","Milk","Curd","Cheese","Milk","Paneer"),
                   PRICE = c(100.981,80.643,80.223,90.3,65.3,71.9,62.2,71.3,25.1,62.9,41.9,35.7,50.9,121.7))

items

The output items data frame is


ITEMS ITEM_NAME   PRICE
1      Fruit     Apple 100.981
2      Fruit    Banana  80.643
3      Fruit    Orange  80.223
4      Fruit     Mango  90.300
5      Fruit    Papaya  65.300
6  Vegetable    Carrot  71.900
7  Vegetable    Potato  62.200
8  Vegetable   Brinjal  71.300
9  Vegetable   Raddish  25.100
10     Dairy      Milk  62.900
11     Dairy      Curd  41.900
12     Dairy    Cheese  35.700
13     Dairy      Milk  50.900
14     Dairy    Paneer 121.700

By applying the range function over the data frame items, we get maximum and minimum values.


range(items$PRICE)
[1]  25.1 121.7

Finding Max and Min values in R

The range in the R programming language can also be determined using min() and max() functions. The min() function finds the minimum data value in the input vector or datatype. The max() function finds the maximum data value from the given input.

Syntax for min() and max() in R


min(<vector>,na.rm=TRUE)       #To find minimum
max(<vector>,na.rm=TRUE)       #To find maximum

A simple program using min() and max() in R where na.rm is to exclude if NA is present. A vector x of numeric data type is created using c() and returns Inf for max() and -1 for min() functions.


x = c(24,5,Inf,6,7,7,-1,0,NA)
> print(max(x,na.rm=TRUE))
[1] Inf
> print(min(x, na.rm=TRUE))
[1] -1

Finding range in R using min() and max() functions

The range of an observation can be calculated by finding the difference between the highest and lowest values. The same theory is applicable to finding the range of a vector or data frame from its given inputs.

Syntax to find range using max() and min()


Range = max(<vector>,na.rm=TRUE)- min(<vector>,na.rm=TRUE)

Program to find range using min() and max()


#Created a vector of numeric data type using c()
 x = c(24,5,4,6,7,7,-1,0,NA)
 print(x)

# find range
 print(max(x, na.rm=TRUE)-min(x, na.rm=TRUE))

The output of the R source code is


[1] 24  5  4  6  7  7 -1  0 NA
[1] 25

How to find the range in columns of the data frame?

The range in column data frames can also be determined using maximum(max) and minimum(min) functions as we do with vectors. The highest value in any particular column of the data frame can be obtained using the max() function. The lowest value of the column in the data frame can be obtained using the min() function. In both cases, the NA values can be excluded using na.rm=TRUE.

Syntax


max(dataframe$column_name,na.rm=TRUE)-min(dataframe$column_name,na.rm=TRUE)

Program to find the range in a data frame.


# create dataframe
x = data.frame(first=c(98, 5, 67, 67, 23, 45, 78, NA, 89),                sec 41, 96, 34, 27, 23, NA, 73,NA))

# display
print(x)

The above R code creates a data frame using two columns named “first” and “second”. Let us find the range of two columns.


# find range in column1
print(max(x$first, na.rm=TRUE)-min(x$first, na.rm=TRUE))

# find range in column2
print(max(x$second, na.rm=TRUE)-min(x$second, na.rm=TRUE))

The execution of the above code displays


[1] 93
[1] 73

How to find the entire data frame range?

The entire data frame range is obtained by finding the difference between maximum and minimum values present which is further determined using max() and min() functions.

Syntax


max(dataframe,na.rm=TRUE)-min(dataframe,na.rm=TRUE)

Program to find the entire range in a data frame


# create dataframe
x = data.frame(first=c(98, 5, 67, 67, 23, 45, 78, NA, 89),                sec 41, 96, 34, 27, 23, NA, 73,NA))

# display
print(x)

# find range in entire dataframe
print(max(x, na.rm=TRUE)-min(x, na.rm=TRUE))

Output


[1] 93