The Wayback Machine - https://web.archive.org/web/20240828110754/https://www.geeksforgeeks.org/get-the-minimum-and-maximum-element-of-a-vector-in-r-programming-range-function/
Open In App

Get the Minimum and Maximum element of a Vector in R Programming – range() Function

Last Updated : 16 Dec, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

range() function in R Programming Language is used to get the minimum and maximum values of the vector passed to it as an argument.

Syntax: range(x, na.rm, finite)

Parameters: 

  • x: Numeric Vector
  • na.rm: Boolean value to remove NA
  • finite: Boolean value to exclude non-finite elements

R – range() Function Examples

Example 1: Get the Minimum and Maximum element of a Vector using range() Function in R language

R




# R program to find the
# minimum and maximum element of a vector
 
# Creating a vector
x <- c(8, 2, 5, 4, 9, 6, 54, 18)
 
# Calling range() function
range(x)


Output: 

[1]  2 54

Example 2: Using range() Function in R programming with NA 

R




# R program to find the
# minimum and maximum element of a vector
 
# Creating a vector
x <- c(8, 2, Inf, 5, 4, NA, 9, 54, 18)
 
# Calling range() function
range(x)
 
# Calling range() function
# excluding NA values
range(x, na.rm = TRUE)
 
# Calling range() function
# excluding finite values
range(x, na.rm = TRUE, finite = TRUE)


Output: 

[1] NA NA
[1]   2 Inf
[1]  2 54


Similar Reads

Convert an Object into a Vector in R Programming - as.vector() Function
as.vector() function in R Language is used to convert an object into a vector. Syntax: as.vector(x) Parameters: x: Object to be converted Example 1: # R program to convert an object to vector # Creating an array x &lt;- array(c(2, 3, 4, 7, 2, 5), c(3, 2)) x # Calling as.vector() Function as.vector(x) Output: [, 1] [, 2] [1, ] 2 7 [2, ] 3 2 [3, ] 4
1 min read
Check for the Existence of a Vector Object in R Programming - is.vector() Function
is.vector() function in R Language is used to return TRUE if the given vector is of the specified mode having no attributes other than names. It returns FALSE otherwise. Syntax: is.vector(x, mode = "any") Parameters: x: R object mode: character string naming an atomic mode or "list" or "expression" or "any" Example 1: # R program to illustrate # is
1 min read
Create a Vector of Colors from a Vector of Gray Levels in R Programming - gray() or grey() Function
gray() or grey() function in R Language is used to create a vector of colors from a vector of gray levels. Syntax: gray(level) or grey(level) Parameters: level: a vector of desired gray levels between 0 and 1; zero indicates "black" and one indicates "white". Example 1: # R program to illustrate # gray function # Calling the gray() function gray(0)
1 min read
Get or Set the Structure of a Vector in R Programming - structure() Function
structure() function in R Language is used to get or set the structure of a vector by changing its dimensions. Syntax: structure(vec, dim) Parameters: vec: Vector dim: New Dimensions Example 1: # R program to get # the structure of a Vector # Creating a Vector # of Numbers x1 &lt;- c(1:10) structure(x1) # Creating a vector # of strings x2 &lt;- c(
1 min read
Get the Sign of Elements of a Numeric Vector in R Programming - sign() Function
sign() function in R Language is used to find the sign of the elements of the numeric vector provided as argument. It does not work on complex vectors. Syntax: sign(x) Parameter: x represents numeric vector Returns: 1 for Positive number and -1 for Negative Number Example 1: # Create a variable x &lt;- 1 y &lt;- -100 # Check sign cat(&quot;Sign of
1 min read
Get the Maximum element of an Object in R Programming - max() Function
max() function in R Language is used to find the maximum element present in an object. This object can be a Vector, a list, a matrix, a data frame, etc.. Syntax: max(object, na.rm) Parameters: object: Vector, matrix, list, data frame, etc. na.rm: Boolean value to remove NA element. Example 1: # R program to illustrate # the use of max() function #
1 min read
Get the position of the maximum element in each Row of a Matrix in R Programming - max.col() Function
max.col() function in R Language check for maximum value in each row and returns the column no. for it. Syntax: max.col(x, ties.method) Parameters: x: Numeric matrix ties.method: It takes random, first, and last as value and returns the position accordingly in case of a tie. Example 1: # R program to find positions # of maximum elements of a matrix
1 min read
Get the Minimum element of an Object in R Programming - min() Function
min() function in R Language is used to find the minimum element present in an object. This object can be a Vector, a list, a matrix, a data frame, etc.. Syntax: min(object, na.rm) Parameters: object: Vector, matrix, list, data frame, etc. na.rm: Boolean value to remove NA element. Example 1: # R program to illustrate # the use of min() function #
1 min read
Getting Match of an Element within a Vector in R Programming - charmatch() Function
charmatch() function in R Programming Language is used to find matches between two arguments. Syntax: charmatch(x, table, nomatch = NA_integer_) Parameters: x: the values to be matchedtable: the values to be matched againstnomatch: the integer value to be returned at non-matching positionr - charmatch() Function Example Example 1: C/C++ Code # R pr
1 min read
Return the Index of the First Maximum Value of a Numeric Vector in R Programming - which.max() Function
which.max() function in R Language is used to return the location of the first maximum value in the Numeric Vector. Syntax: which.max(x) Parameters: x: Numeric Vector Example 1: # R program to find index of # first maximum value # Creating a vector x &lt;- c(2, 3, 4, 5, 1, 2, 3, 1, 2) # Calling which.max() function which.max(x) # Printing maximum v
1 min read
Return the Index of the First Minimum Value of a Numeric Vector in R Programming - which.min() Function
which.min() function in R Language is used to return the location of the first minimum value in the Numeric Vector. Syntax: which.min(x) Parameters: x: Numeric Vector Example 1: # R program to find index of # first minimum value # Creating a vector x &lt;- c(2, 3, 4, 5, 1, 2, 3, 1, 2) # Calling which.min() function which.min(x) # Printing minimum v
1 min read
Search the Interval for Minimum and Maximum of the Function in R Programming - optimize() Function
optimize() or optimise() function in R Language is used to search the interval from lower to upper for a minimum or maximum of the function f with respect to its first argument. Syntax: optimize(f, interval, maximum)Parameters: f: the function to be optimized. The function is either minimized or maximized over its first argument depending on the va
2 min read
Get a List of Numbers in the Specified Range in R Programming - seq.int() Function
seq.int() function in R Language is used to return a list of numbers in the specified range. Syntax: seq.int(from, to, by) Parameters: from: specified range from to: specified range to by: specified number by which it jumps through returned number Example 1: # R program to illustrate # seq.int function # Calling seq.int() function seq.int(0, 5) seq
1 min read
Add Leading Zeros to the Elements of a Vector in R Programming - Using paste0() and sprintf() Function
paste0() and sprintf() functions in R Language can also be used to add leading zeros to each element of a vector passed to it as argument. Syntax: paste0("0", vec) or sprintf("%0d", vec)Parameters: paste0: It will add zeros to vector sprintf: To format a vector(adding zeros) vec: Original vector dataReturns: Vectors by addition of leading zeros Exa
1 min read
Check if values in a vector are True or not in R Programming - all() and any() Function
In this article, we are going to check if the values in a vector are true or not in R Programming Language. R - all() function all() function in R Language will check in a vector whether all the values are true or not. Syntax: all(x, na.rm) Parameters: x: vectorna.rm: logical, if NA value to removed before result Example 1: Basic example of all() f
2 min read
Check if the elements of a Vector are Finite, Infinite or NaN values in R Programming - is.finite(), is.infinite() and is.nan() Function
is.finite() function in R Language is used to check if the elements of a vector are Finite values or not. It returns a boolean value for all the elements of the vector. Syntax: is.finite(x) Parameters: x: Vector to be checked Example: # R program to illustrate # the use of is.finite() function # Creating a vector x &lt;- c(1, 2, 3, 4, 5, NA, 6, 7)
2 min read
Create a Vector of Colors with specified Hue, Chroma and Luminance in R Programming - hcl() Function
hcl() function in R Language is used to create a vector of colors from vectors specifying hue, chroma and luminance. Syntax: hcl(h, c, l) Parameters: h: The hue of the colour specified as an angle in the range [0, 360]. 0 yields red, 120 yields green 240 yields blue, etc. c: The chroma of the colour. The upper bound for chroma depends on hue and lu
2 min read
Create a Vector of Colors with specified Hue, Saturation and Value in R Programming - hsv() Function
hsv() function in R Language is used to create a vector of colors from vectors specifying hue, saturation and value. Syntax: hsv(h, s, v) Parameters: h, s, v: numeric vectors of values in the range [0, 1] for ‘hue’, ‘saturation’ and ‘value’ to be combined to form a vector of colors. Example 1: # R program to illustrate # hsv function # Calling the
1 min read
How to create a new vector from a given vector in R
In this article, we will discuss How to create a new vector from a given vector in R Programming Language. Create a new vector from a given vectorYou can use various functions and techniques depending on your needs to create a new vector from a given vector in R. Here are some common methods. 1. Subset the VectorIf you want to create a new vector w
2 min read
Check if Elements of a Vector are non-empty Strings in R Programming - nzchar() Function
nzchar() function in R Language is used to test whether elements of a character vector are non-empty strings. Syntax: nzchar(x) Parameters: x: character vector Example 1: # R program to illustrate # nzchar function # Initializing a character vector x &lt;- c(&quot;GFG&quot;, &quot;gfg&quot;) y &lt;- c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;) #
1 min read
Make Elements of a Vector Unique in R Programming - make.unique() Function
make.unique() function in R Language is used to return elements of a vector with unique names by appending sequence numbers to duplicates. Syntax: make.unique(names, sep)Parameters: names: Character vector with duplicate names sep: Separator to be used Example 1: C/C++ Code # R program to make unique vectors # Calling make.unique() Function make.un
1 min read
Compute the gamma value of a Non-negative Numeric Vector in R Programming - gamma() Function
gamma() function in R Language is used to compute the gamma value of a numeric vector using the gamma function. Gamma function is basically, gamma(x) = factorial(x - 1) Syntax: gamma(x) Parameters: x: Non-negative Numeric Vector Example 1: # R program to calculate the gamma value # Calling gamma Function gamma(2) gamma(3) gamma(5) Output: [1] 1 [1]
1 min read
Replace the Elements of a Vector in R Programming - replace() Function
replace() function in R Language is used to replace the values in the specified string vector x with indices given in list by those given in values. Syntax: replace(x, list, values) Parameters: x: vector list: indices values: replacement values Example 1: # R program to illustrate # replace function # Initializing a string vector x &lt;- c(&quot;GF
1 min read
Extracting Substrings from a Character Vector in R Programming - substring() Function
substring() function in R Programming Language is used to extract substrings in a character vector. You can easily extract the required substring or character from the given string. Syntax: substring(text, first, last) Parameters: text: character vectorfirst: integer, the first element to be replacedlast: integer, the last element to be replacedR -
2 min read
Find String Matches in a Vector or Matrix in R Programming - str_detect() Function
str_detect() Function in R Language is used to check if the specified match of the substring exists in the original string. It will return TRUE for a match found otherwise FALSE against each of the element of the Vector or matrix. Note: This function uses 'stringr' Library. Syntax: str_detect(string, pattern) Parameter: string: specified string pat
1 min read
Convert a Vector into Factor in R Programming - as.factor() Function
as.factor() function in R Programming Language is used to convert the passed object(usually Vector) into a Factor. Syntax: as.factor(object) Parameters: Object: Vector to be convertedas.factor() Function in R ExampleExample 1: Convert a Factor in R C/C++ Code # Creating a vector x&lt;-c(&quot;female&quot;, &quot;male&quot;, &quot;male&quot;, &quot;
1 min read
Calculate the difference between Consecutive pair of Elements of a Vector in R Programming - diff() Function
diff() function in R Language is used to find the difference between each consecutive pair of elements of a vector. Syntax: diff(x, lag, differences) Parameters: x: vector or matrix lag: period between elements differences: Order of difference Example 1: # R program to find the difference # between each pair of elements of a vector # Creating a vec
2 min read
Calculate Median of elements of a Vector in R Programming - median() Function
median() function in R Language is used to calculate the median of the elements of the numeric vector passed as argument. Syntax: median(x, na.rm) Parameters: x: Numeric Vector na.rm: Boolean value to ignore NA Example 1: # R program to calculate median of a vector # Creating vector x1 &lt;- c(2, 3, 5, 7, 4, 8, 6) x2 &lt;- c(-3, -5, 6, 2, -4) # Cal
1 min read
Calculate Rank of the Values of a Vector in R Programming - rank() Function
rank() function in R Language is used to return the sample ranks of the values of a vector. Equal values and missing values are handled in multiple ways. Syntax: rank(x, na.last) Parameters: x: numeric, complex, character, and logical vector na.last: Boolean Value to remove NAs Example 1: # R program to print rank of a vector values # Creating a ve
1 min read
Combine Arguments into a Vector in R Programming - c() Function
c() function in R Language is used to combine the arguments passed to it. Syntax: c(...) Parameters: ...: arguments to be combined Example 1: # R program to cumulative maxima # Calling c() function x &lt;- c(1, 2, 3, 4) x Output: [1] 1 2 3 4 Example 2: # R program to cumulative maxima # Calling c() function x &lt;- c(&quot;a&quot;, &quot;b&quot;,
1 min read
Article Tags :