Range Function in R

Range function in R, returns a vector containing the minimum and maximum of all the given arguments.

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 in R 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 R:

# R Range function for a character vector

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

range function in r                                                                                                                range function in R