R Program to find maximum and minimum value of a given vector


February 5, 2023, Learn eTutorial
1702

What is a vector in R? 

Vector can be defined as a data type that includes a list of values of the same type. each value in a vector is called its components, separated by commas. For example, consider C = (1, 2, 3, 4, 5, 6) and it is a vector that contains a list of values of int type. 

How to find the maximum and minimum vector in R program?

To find a vector maximum and minimum using the R program, we are using built-in functions max, min for this calculation. The values are passed to these functions directly here.

  • min() function in R computes the minimum value or data frame.
  • max() function in R computes the maximum value or data frame.

min(x, na.rm = FALSE)
max(x, na.rm = FALSE)

#where.

  • x is a numeric or character vector
  • na.rm – a logical indicating whether missing values should be removed

ALGORITHM

STEP 1: Assign variable num with vector values

STEP 2: Use the built-in functions

STEP 3: First print the original values

STEP 4: call max() with an argument as num to find the maximum value

STEP 5: call min() with an argument as num to find the minimum value

STEP 6: print the result of each function

R Source Code

                                          num = c(5, 10, 15, 20, 25, 30)
print('Original vector is:')
print(num)   
print(paste("Maximum value of the said vector:",max(num)))
print(paste("Minimum value of the said vector:",min(num)))
                                      

OUTPUT

[1] "Original vector is:"
[1] 5, 10, 15, 20, 25, 30
[1] "Maximum value of the said vector: 30"
[1] "Minimum value of the said vector: 5"