Skip to content
Related Articles

Related Articles

Calculate Rank of the Values of a Vector in R Programming – rank() Function
  • Last Updated : 15 Jun, 2020

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
My Personal Notes arrow_drop_up
Recommended Articles
Page :