w3resource

Find Minimum and Maximum Values of a Vector in R Programming


Write a R program to find the minimum and the maximum of a Vector.

Sample Solution :

R Programming Code :

# Create a numeric vector 'x' with elements 10, 20, 30, 25, 9, and 26
x = c(10, 20, 30, 25, 9, 26)

# Print a message indicating the display of the original vector
print("Original Vectors:")

# Print the contents of vector 'x'
print(x)

# Print a message indicating the calculation of the maximum value
print("Maximum value of the above Vector:")

# Calculate and print the maximum value in vector 'x'
print(max(x))

# Print a message indicating the calculation of the minimum value
print("Minimum value of the above Vector:")

# Calculate and print the minimum value in vector 'x'
print(min(x))

Output:

[1] "Original Vectors:"
[1] 10 20 30 25  9 26
[1] "Maximum value of the above Vector:"
[1] 30
[1] "Minimum value of the above Vector:"
[1] 9                         

Explanation:

  • x = c(10, 20, 30, 25, 9, 26): Creates a numeric vector x with the elements 10, 20, 30, 25, 9, and 26.
  • print("Original Vectors:"): Prints the label "Original Vectors:" to indicate that the following output will display the vector x.
  • print(x): Prints the contents of vector x.
  • print("Maximum value of the above Vector:"): Prints the label "Maximum value of the above Vector:" to indicate that the following output will show the maximum value of vector x.
  • print(max(x)): Calculates and prints the maximum value in vector x, which is 30.
  • print("Minimum value of the above Vector:"): Prints the label "Minimum value of the above Vector:" to indicate that the following output will show the minimum value of vector x.
  • print(min(x)): Calculates and prints the minimum value in vector x, which is 9.

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to find Sum, Mean and Product of a Vector, ignore element like NA or NaN.
Next: Write a R program to sort a Vector in ascending and descending order.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.