Range Function in R – range()

Range function in R, returns a vector containing the minimum and maximum of all the given arguments. Range() function in R returns the maximum and minimum value of the vector and column of the dataframe in R. range() function of the column of dataframe.  The range is the interval between the lowest and the highest value within the data vector and the column of the dataframe.

  • range() function is used to find the lowest and highest value of the vector
  • range() function of a vector with NA values  by using na.rm = FALSE
  • Highest and lowest value of the column in dataframe is also accomplished using range() function.

 

Syntax for Range function in R:

range(x, na.rm = FALSE)
  • x can be a character or numeric vector
  • Whether NA should be removed, if not, NA will be returned.

Example of Range function in R:

Range function returns the minimum and maximum value from the input vector

# R Range function - for a numeric vector

x <- c(2,7,1,-17,Inf,35,21,7)
range(x)
output:
[1]  -17   Inf

 

 

Example of Range function with NA:

Range function will never give the result if NA is present in the input vector. So we have to use na.rm=TRUE option, to get the desired result

# R Range function - for a numeric vector with NA

x <- c(2,7,1,-17,Inf,35,21,7,NA)
range(x,na.rm=TRUE)
output:
[1]  -17   Inf

 

 

Range function for a character vector:

In the below example range() function is applied to the character vector, which it takes up the vector name as argument and results in alphabetical highest and lowest value of the character vector.

# R Range function for a character vector

x<-c("a","h","x","i","j")
range(x)
output:
[1]  “a”   “x”

 

 

Example of range() function in R of the dataframe column:

we will also picking up a quick example on how to apply range() function to a column of the dataframe. In order to depict that lets first create a dataframe

#### Create dataframe in R

my_basket = data.frame(ITEM_GROUP = 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))

my_basket
 

so the resultant dataframe will be

round function in R 11

 

 

Example of range() function of column of a dataframe : 

In the below example range() function is applied to the column of a dataframe in which it takes up the column name as argument. which finds the highest and lowest value of the column.

# range of the column in R range() function

range(my_basket$Price)

so in the “my_basket” dataframe, “price” column will be taken as an argument to the range function and as a result  highest and lowest value of “price” column is computed as shown below.

output:
[1] 25.1   121.7

 

range function in r                                                                                                         range function in R

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts