Open In App

Calculate Rank of the Values of a Vector in R Programming – rank() Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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 vector
x <- c(2, 5, 3, 6, -4, NA, 7, Inf, 5)
  
# Calling rank() function
rank(x)


Output:

[1] 2.0 4.5 3.0 6.0 1.0 9.0 7.0 8.0 4.5

Example 2:




# R program to print rank of a vector values
  
# Creating a vector
x <- c(2, 5, 3, 6, -4, NA, 7, Inf, 5)
  
# Calling rank() function
rank(x)
  
# Calling with NA removed
rank(x, na.last = FALSE)


Output:

[1] 2.0 4.5 3.0 6.0 1.0 9.0 7.0 8.0 4.5
[1] 3.0 5.5 4.0 7.0 2.0 1.0 8.0 9.0 5.5

Last Updated : 15 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads