w3resource

Find the nth Highest value in a Vector using R Programming

R Programming: Vector Exercise-14 with Solution

Write a R program to find nth highest value in a given vector.

Sample Solution :

R Programming Code :

# Create a numeric vector 'x' with elements 10, 20, 30, 20, 20, 25, 9, and 26
x = c(10, 20, 30, 20, 20, 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 that the nth highest value will be shown
print("nth highest value in a given vector:")

# Print a message for the nth highest value where n = 1
print("n = 1")

# Set 'n' to 1 (highest value) and print the 1st highest value in vector 'x'
n = 1
print(sort(x, TRUE)[n])

# Print a message for the nth highest value where n = 2
print("n = 2")

# Set 'n' to 2 (second highest value) and print the 2nd highest value in vector 'x'
n = 2
print(sort(x, TRUE)[n])

# Print a message for the nth highest value where n = 3
print("n = 3")

# Set 'n' to 3 (third highest value) and print the 3rd highest value in vector 'x'
n = 3
print(sort(x, TRUE)[n])

# Print a message for the nth highest value where n = 4
print("n = 4")

# Set 'n' to 4 (fourth highest value) and print the 4th highest value in vector 'x'
n = 4
print(sort(x, TRUE)[n])

Output:

[1] "Original Vectors:"
[1] 10 20 30 20 20 25  9 26
[1] "nth highest value in a given vector:"
[1] "n = 1"
[1] 30
[1] "n = 2"
[1] 26
[1] "n = 3"
[1] 25
[1] "n = 4"
[1] 20                         

Explanation:

  • Define Vector:
    • x = c(10, 20, 30, 20, 20, 25, 9, 26)
    • Creates a numeric vector x with the values 10, 20, 30, 20, 20, 25, 9, and 26.
  • Print Original Vector:
    • print("Original Vectors:")
    • Displays the message "Original Vectors:".
    • print(x)
    • Prints the contents of vector x.
  • Print nth Highest Values:
    • print("nth highest value in a given vector:")
    • Displays the message "nth highest value in a given vector:".
    • For n = 1:
      • print("n = 1")
      • Displays the message "n = 1".
      • n = 1
      • Sets n to 1.
      • print(sort(x, TRUE)[n])
      • Sorts vector x in descending order and prints the 1st highest value.
    • For n = 2:
      • print("n = 2")
      • Displays the message "n = 2".
      • n = 2
      • Sets n to 2.
      • print(sort(x, TRUE)[n])
      • Sorts vector x in descending order and prints the 2nd highest value.
    • For n = 3:
      • print("n = 3")
      • Displays the message "n = 3".
      • n = 3
      • Sets n to 3.
      • print(sort(x, TRUE)[n])
      • Sorts vector x in descending order and prints the 3rd highest value.
    • For n = 4:
      • print("n = 4")
      • Displays the message "n = 4".
      • n = 4
      • Sets n to 4.
      • print(sort(x, TRUE)[n])
      • Sorts vector x in descending order and prints the 4th highest value.

    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 second highest value in a given vector.
    Next: Wrtie a R program to find common elements from multiple vectors.

    Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/r-programming-exercises/vector/r-programming-vector-exercise-14.php