Open In App

How to find Nth smallest value in vector in R ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to find the Nth smallest in vector in the R programming language.

Steps –

  • Create vector
  • Take input from the user using the function readline().
  • Convert data from string to int using the function as.integer().
  • In this step, we are finding nth largest number using

Syntax:

sort(vector name ) [n value])

Parameter:

  • Vector name is the vector which we created.
  • N value is which largest number you want to print.

Example 1:

R




a<- c(1,3,4)
  
x = readline();
x = as.integer(x);
  
print(sort(a)[x])


Output:

[1] 1

Example 2:

R




a<- c(3453,39898,-997784)
  
x = readline();
x = as.integer(x);
  
print(sort(a)[x])


Output:

[1] 39898


Last Updated : 07 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads