Calculate Rank of the Values of a Vector in R Programming – rank() Function
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 vectorx <- c(2, 5, 3, 6, -4, NA, 7, Inf, 5) # Calling rank() functionrank(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 vectorx <- c(2, 5, 3, 6, -4, NA, 7, Inf, 5) # Calling rank() functionrank(x) # Calling with NA removedrank(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


